• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "locator_ability.h"
17 
18 #include "accesstoken_kit.h"
19 #include "event_runner.h"
20 #include "system_ability_definition.h"
21 #include "switch_callback_proxy.h"
22 
23 #include "common_event_manager.h"
24 #include "common_hisysevent.h"
25 #include "common_utils.h"
26 #include "constant_definition.h"
27 #include "country_code.h"
28 #include "geo_convert_proxy.h"
29 #include "gnss_ability_proxy.h"
30 #include "locator_background_proxy.h"
31 #include "location_config_manager.h"
32 #include "location_log.h"
33 #include "network_ability_proxy.h"
34 #include "passive_ability_proxy.h"
35 #include "permission_status_change_cb.h"
36 
37 namespace OHOS {
38 namespace Location {
39 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(
40     DelayedSingleton<LocatorAbility>::GetInstance().get());
41 
42 const uint32_t EVENT_UPDATE_SA = 0x0001;
43 const uint32_t EVENT_INIT_REQUEST_MANAGER = 0x0002;
44 const uint32_t EVENT_APPLY_REQUIREMENTS = 0x0003;
45 const uint32_t EVENT_RETRY_REGISTER_ACTION = 0x0004;
46 const uint32_t RETRY_INTERVAL_UNITE = 1000;
47 const uint32_t RETRY_INTERVAL_OF_INIT_REQUEST_MANAGER = 5 * RETRY_INTERVAL_UNITE;
48 const uint32_t SET_ENABLE = 3;
49 const uint32_t REG_GNSS_STATUS = 7;
50 const uint32_t UNREG_GNSS_STATUS = 8;
51 const uint32_t REG_NMEA = 9;
52 const uint32_t UNREG_NMEA = 10;
53 const uint32_t REG_CACHED = 11;
54 const uint32_t UNREG_CACHED = 12;
55 const uint32_t GET_CACHED_SIZE = 13;
56 const uint32_t FLUSH_CACHED = 14;
57 const uint32_t SEND_COMMANDS = 15;
58 const uint32_t ADD_FENCE_INFO = 16;
59 const uint32_t REMOVE_FENCE_INFO = 17;
60 const float_t PRECISION = 0.000001;
61 
LocatorAbility()62 LocatorAbility::LocatorAbility() : SystemAbility(LOCATION_LOCATOR_SA_ID, true)
63 {
64     switchCallbacks_ = std::make_unique<std::map<pid_t, sptr<ISwitchCallback>>>();
65     requests_ = std::make_shared<std::map<std::string, std::list<std::shared_ptr<Request>>>>();
66     receivers_ = std::make_shared<std::map<sptr<IRemoteObject>, std::list<std::shared_ptr<Request>>>>();
67     proxyMap_ = std::make_shared<std::map<std::string, sptr<IRemoteObject>>>();
68     permissionMap_ = std::make_shared<std::map<uint32_t, std::shared_ptr<PermissionStatusChangeCb>>>();
69     InitRequestManagerMap();
70     reportManager_ = DelayedSingleton<ReportManager>::GetInstance();
71     LBSLOGI(LOCATOR, "LocatorAbility constructed.");
72 }
73 
~LocatorAbility()74 LocatorAbility::~LocatorAbility() {}
75 
OnStart()76 void LocatorAbility::OnStart()
77 {
78     if (state_ == ServiceRunningState::STATE_RUNNING) {
79         LBSLOGI(LOCATOR, "LocatorAbility has already started.");
80         return;
81     }
82     if (!Init()) {
83         LBSLOGE(LOCATOR, "failed to init LocatorAbility");
84         OnStop();
85         return;
86     }
87     state_ = ServiceRunningState::STATE_RUNNING;
88     AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
89     LBSLOGI(LOCATOR, "LocatorAbility::OnStart start ability success.");
90 }
91 
OnStop()92 void LocatorAbility::OnStop()
93 {
94     state_ = ServiceRunningState::STATE_NOT_START;
95     registerToAbility_ = false;
96     LBSLOGI(LOCATOR, "LocatorAbility::OnStop ability stopped.");
97 }
98 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)99 void LocatorAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
100 {
101     if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
102         LBSLOGE(LOCATOR, "systemAbilityId is not COMMON_EVENT_SERVICE_ID");
103         return;
104     }
105     if (locatorEventSubscriber_ == nullptr) {
106         LBSLOGE(LOCATOR, "OnAddSystemAbility subscribeer is nullptr");
107         return;
108     }
109     OHOS::EventFwk::MatchingSkills matchingSkills;
110     matchingSkills.AddEvent(MODE_CHANGED_EVENT);
111     OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
112     bool result = OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(locatorEventSubscriber_);
113     LBSLOGI(LOCATOR, "SubscribeCommonEvent locatorEventSubscriber_ result = %{public}d", result);
114     if (countryCodeManager_ == nullptr) {
115         countryCodeManager_ = DelayedSingleton<CountryCodeManager>::GetInstance();
116     }
117     if (countryCodeManager_ != nullptr) {
118         countryCodeManager_->ReSubscribeEvent();
119     }
120 }
121 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)122 void LocatorAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
123 {
124     if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
125         LBSLOGE(LOCATOR, "systemAbilityId is not COMMON_EVENT_SERVICE_ID");
126         return;
127     }
128     if (locatorEventSubscriber_ == nullptr) {
129         LBSLOGE(LOCATOR, "OnRemoveSystemAbility subscribeer is nullptr");
130         return;
131     }
132     bool result = OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(locatorEventSubscriber_);
133     LBSLOGI(LOCATOR, "UnSubscribeCommonEvent locatorEventSubscriber_ result = %{public}d", result);
134     if (countryCodeManager_ == nullptr) {
135         countryCodeManager_ = DelayedSingleton<CountryCodeManager>::GetInstance();
136     }
137     if (countryCodeManager_ != nullptr) {
138         countryCodeManager_->ReUnsubscribeEvent();
139     }
140 }
141 
Init()142 bool LocatorAbility::Init()
143 {
144     if (registerToAbility_) {
145         return true;
146     }
147     LBSLOGI(LOCATOR, "LocatorAbility Init.");
148     bool ret = Publish(AsObject());
149     if (!ret) {
150         LBSLOGE(LOCATOR, "Init add system ability failed!");
151         return false;
152     }
153 
154     deviceId_ = CommonUtils::InitDeviceId();
155     requestManager_ = DelayedSingleton<RequestManager>::GetInstance();
156     locatorHandler_ = std::make_shared<LocatorHandler>(AppExecFwk::EventRunner::Create(true));
157     if (countryCodeManager_ == nullptr) {
158         countryCodeManager_ = DelayedSingleton<CountryCodeManager>::GetInstance();
159     }
160     InitSaAbility();
161     if (locatorHandler_ != nullptr) {
162         locatorHandler_->SendHighPriorityEvent(EVENT_INIT_REQUEST_MANAGER, 0, RETRY_INTERVAL_OF_INIT_REQUEST_MANAGER);
163     }
164     RegisterAction();
165     registerToAbility_ = true;
166     return registerToAbility_;
167 }
168 
LocatorHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner)169 LocatorHandler::LocatorHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner) : EventHandler(runner) {}
170 
~LocatorHandler()171 LocatorHandler::~LocatorHandler() {}
172 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)173 void LocatorHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
174 {
175     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
176     auto requestManager = DelayedSingleton<RequestManager>::GetInstance();
177     uint32_t eventId = event->GetInnerEventId();
178     LBSLOGI(LOCATOR, "ProcessEvent event:%{public}d", eventId);
179     switch (eventId) {
180         case EVENT_UPDATE_SA: {
181             if (locatorAbility != nullptr) {
182                 locatorAbility->UpdateSaAbilityHandler();
183             }
184             break;
185         }
186         case EVENT_RETRY_REGISTER_ACTION: {
187             if (locatorAbility != nullptr) {
188                 locatorAbility->RegisterAction();
189             }
190             break;
191         }
192         case EVENT_INIT_REQUEST_MANAGER: {
193             if (requestManager == nullptr || !requestManager->InitSystemListeners()) {
194                 LBSLOGE(LOCATOR, "InitSystemListeners failed");
195             }
196             break;
197         }
198         case EVENT_APPLY_REQUIREMENTS: {
199             if (requestManager != nullptr) {
200                 requestManager->HandleRequest();
201             }
202             break;
203         }
204         default:
205             break;
206     }
207 }
208 
InitRequestManagerMap()209 void LocatorAbility::InitRequestManagerMap()
210 {
211     if (requests_ != nullptr) {
212         std::list<std::shared_ptr<Request>> gnssList;
213         requests_->insert(make_pair(GNSS_ABILITY, gnssList));
214         std::list<std::shared_ptr<Request>> networkList;
215         requests_->insert(make_pair(NETWORK_ABILITY, networkList));
216         std::list<std::shared_ptr<Request>> passiveList;
217         requests_->insert(make_pair(PASSIVE_ABILITY, passiveList));
218     }
219 }
220 
GetRequests()221 std::shared_ptr<std::map<std::string, std::list<std::shared_ptr<Request>>>> LocatorAbility::GetRequests()
222 {
223     return requests_;
224 }
225 
GetActiveRequestNum()226 int LocatorAbility::GetActiveRequestNum()
227 {
228     int num = 0;
229     auto gpsListIter = requests_->find(GNSS_ABILITY);
230     auto networkListIter = requests_->find(NETWORK_ABILITY);
231     if (gpsListIter != requests_->end()) {
232         auto list = &(gpsListIter->second);
233         num += static_cast<int>(list->size());
234     }
235     if (networkListIter != requests_->end()) {
236         auto list = &(networkListIter->second);
237         num += static_cast<int>(list->size());
238     }
239     return num;
240 }
241 
GetReceivers()242 std::shared_ptr<std::map<sptr<IRemoteObject>, std::list<std::shared_ptr<Request>>>> LocatorAbility::GetReceivers()
243 {
244     return receivers_;
245 }
246 
GetProxyMap()247 std::shared_ptr<std::map<std::string, sptr<IRemoteObject>>> LocatorAbility::GetProxyMap()
248 {
249     return proxyMap_;
250 }
251 
ApplyRequests()252 void LocatorAbility::ApplyRequests()
253 {
254     if (locatorHandler_ != nullptr) {
255         locatorHandler_->SendHighPriorityEvent(EVENT_APPLY_REQUIREMENTS, 0, RETRY_INTERVAL_UNITE);
256     }
257 }
258 
InitSaAbility()259 void LocatorAbility::InitSaAbility()
260 {
261     LBSLOGI(LOCATOR, "initSaAbility start");
262     if (proxyMap_ == nullptr) {
263         return;
264     }
265     // init gnss ability sa
266     sptr<IRemoteObject> objectGnss = CommonUtils::GetRemoteObject(LOCATION_GNSS_SA_ID, CommonUtils::InitDeviceId());
267     if (objectGnss != nullptr) {
268         proxyMap_->insert(make_pair(GNSS_ABILITY, objectGnss));
269     } else {
270         LBSLOGE(LOCATOR, "GetRemoteObject gnss sa is null");
271     }
272 
273     // init network ability sa
274     sptr<IRemoteObject> objectNetwork = CommonUtils::GetRemoteObject(LOCATION_NETWORK_LOCATING_SA_ID,
275         CommonUtils::InitDeviceId());
276     if (objectNetwork != nullptr) {
277         proxyMap_->insert(make_pair(NETWORK_ABILITY, objectNetwork));
278     } else {
279         LBSLOGE(LOCATOR, "GetRemoteObject network sa is null");
280     }
281 
282     // init passive ability sa
283     sptr<IRemoteObject> objectPassive = CommonUtils::GetRemoteObject(LOCATION_NOPOWER_LOCATING_SA_ID,
284         CommonUtils::InitDeviceId());
285     if (objectPassive != nullptr) {
286         proxyMap_->insert(make_pair(PASSIVE_ABILITY, objectPassive));
287     } else {
288         LBSLOGE(LOCATOR, "GetRemoteObject passive sa is null");
289     }
290 
291     UpdateSaAbilityHandler();
292 }
293 
CheckSaValid()294 bool LocatorAbility::CheckSaValid()
295 {
296     auto objectGnss = proxyMap_->find(GNSS_ABILITY);
297     if (objectGnss == proxyMap_->end()) {
298         LBSLOGI(LOCATOR, "gnss sa is null");
299         return false;
300     }
301 
302     auto objectNetwork = proxyMap_->find(NETWORK_ABILITY);
303     if (objectNetwork == proxyMap_->end()) {
304         LBSLOGI(LOCATOR, "network sa is null");
305         return false;
306     }
307 
308     auto objectPassive = proxyMap_->find(PASSIVE_ABILITY);
309     if (objectPassive == proxyMap_->end()) {
310         LBSLOGI(LOCATOR, "passive sa is null");
311         return false;
312     }
313 
314     return true;
315 }
316 
UpdateSaAbility()317 LocationErrCode LocatorAbility::UpdateSaAbility()
318 {
319     auto event = AppExecFwk::InnerEvent::Get(EVENT_UPDATE_SA, 0);
320     if (locatorHandler_ != nullptr) {
321         locatorHandler_->SendHighPriorityEvent(event);
322     }
323     return ERRCODE_SUCCESS;
324 }
325 
UpdateSaAbilityHandler()326 void LocatorAbility::UpdateSaAbilityHandler()
327 {
328     int state = QuerySwitchState();
329     LBSLOGI(LOCATOR, "update location subability enable state, switch state=%{public}d, action registered=%{public}d",
330         state, isActionRegistered);
331     bool currentEnable = isEnabled_;
332     isEnabled_ = (state == ENABLED);
333     if (isEnabled_ == currentEnable) {
334         return;
335     }
336     if (proxyMap_ == nullptr) {
337         return;
338     }
339     auto locatorBackgroundProxy = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
340     if (locatorBackgroundProxy == nullptr) {
341         LBSLOGE(LOCATOR, "UpdateSaAbilityHandler: LocatorBackgroundProxy is nullptr");
342         return;
343     }
344     locatorBackgroundProxy.get()->OnSaStateChange(isEnabled_);
345     for (auto iter = proxyMap_->begin(); iter != proxyMap_->end(); iter++) {
346         sptr<IRemoteObject> remoteObject = iter->second;
347         MessageParcel data;
348         if (iter->first == GNSS_ABILITY) {
349             data.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor());
350         } else if (iter->first == NETWORK_ABILITY) {
351             data.WriteInterfaceToken(NetworkAbilityProxy::GetDescriptor());
352         } else if (iter->first == PASSIVE_ABILITY) {
353             data.WriteInterfaceToken(PassiveAbilityProxy::GetDescriptor());
354         }
355         data.WriteBool(isEnabled_);
356 
357         MessageParcel reply;
358         MessageOption option;
359         int error = remoteObject->SendRequest(SET_ENABLE, data, reply, option);
360         LBSLOGD(LOCATOR, "enable %{public}s ability, remote result %{public}d", (iter->first).c_str(), error);
361     }
362     for (auto iter = switchCallbacks_->begin(); iter != switchCallbacks_->end(); iter++) {
363         sptr<IRemoteObject> remoteObject = (iter->second)->AsObject();
364         auto callback = std::make_unique<SwitchCallbackProxy>(remoteObject);
365         callback->OnSwitchChange(state);
366     }
367 }
368 
EnableAbility(bool isEnabled)369 LocationErrCode LocatorAbility::EnableAbility(bool isEnabled)
370 {
371     if (isEnabled_ == isEnabled) {
372         LBSLOGD(LOCATOR, "no need to set location ability, enable:%{public}d", isEnabled_);
373         return ERRCODE_SUCCESS;
374     }
375     LBSLOGI(LOCATOR, "EnableAbility %{public}d", isEnabled);
376     int modeValue = isEnabled ? 1 : 0;
377     LocationConfigManager::GetInstance().SetLocationSwitchState(modeValue);
378     UpdateSaAbility();
379     std::string state = isEnabled ? "enable" : "disable";
380     WriteLocationSwitchStateEvent(state);
381     return ERRCODE_SUCCESS;
382 }
383 
GetSwitchState(int & state)384 LocationErrCode LocatorAbility::GetSwitchState(int& state)
385 {
386     isEnabled_ = (QuerySwitchState() == ENABLED);
387     state = isEnabled_ ? ENABLED : DISABLED;
388     return ERRCODE_SUCCESS;
389 }
390 
QuerySwitchState()391 int LocatorAbility::QuerySwitchState()
392 {
393     return LocationConfigManager::GetInstance().GetLocationSwitchState();
394 }
395 
IsLocationPrivacyConfirmed(const int type,bool & isConfirmed)396 LocationErrCode LocatorAbility::IsLocationPrivacyConfirmed(const int type, bool& isConfirmed)
397 {
398     return LocationConfigManager::GetInstance().GetPrivacyTypeState(type, isConfirmed);
399 }
400 
SetLocationPrivacyConfirmStatus(const int type,bool isConfirmed)401 LocationErrCode LocatorAbility::SetLocationPrivacyConfirmStatus(const int type, bool isConfirmed)
402 {
403     return LocationConfigManager::GetInstance().SetPrivacyTypeState(type, isConfirmed);
404 }
405 
RegisterSwitchCallback(const sptr<IRemoteObject> & callback,pid_t uid)406 LocationErrCode LocatorAbility::RegisterSwitchCallback(const sptr<IRemoteObject>& callback, pid_t uid)
407 {
408     if (callback == nullptr) {
409         LBSLOGE(LOCATOR, "register an invalid switch callback");
410         return ERRCODE_INVALID_PARAM;
411     }
412     sptr<IRemoteObject::DeathRecipient> death(new (std::nothrow) SwitchCallbackDeathRecipient());
413     callback->AddDeathRecipient(death.GetRefPtr());
414     sptr<ISwitchCallback> switchCallback = iface_cast<ISwitchCallback>(callback);
415     if (switchCallback == nullptr) {
416         LBSLOGE(LOCATOR, "cast switch callback fail!");
417         return ERRCODE_INVALID_PARAM;
418     }
419     switchCallbacks_->erase(uid);
420     switchCallbacks_->insert(std::make_pair(uid, switchCallback));
421     LBSLOGD(LOCATOR, "after uid:%{public}d register, switch callback size:%{public}s",
422         uid, std::to_string(switchCallbacks_->size()).c_str());
423     return ERRCODE_SUCCESS;
424 }
425 
UnregisterSwitchCallback(const sptr<IRemoteObject> & callback)426 LocationErrCode LocatorAbility::UnregisterSwitchCallback(const sptr<IRemoteObject>& callback)
427 {
428     if (callback == nullptr) {
429         LBSLOGE(LOCATOR, "unregister an invalid switch callback");
430         return ERRCODE_INVALID_PARAM;
431     }
432     sptr<ISwitchCallback> switchCallback = iface_cast<ISwitchCallback>(callback);
433     if (switchCallback == nullptr) {
434         LBSLOGE(LOCATOR, "cast switch callback fail!");
435         return ERRCODE_INVALID_PARAM;
436     }
437 
438     pid_t uid = -1;
439     for (auto iter = switchCallbacks_->begin(); iter != switchCallbacks_->end(); iter++) {
440         sptr<IRemoteObject> remoteObject = (iter->second)->AsObject();
441         if (remoteObject == callback) {
442             uid = iter->first;
443             break;
444         }
445     }
446     switchCallbacks_->erase(uid);
447     LBSLOGD(LOCATOR, "after uid:%{public}d unregister, switch callback size:%{public}s",
448         uid, std::to_string(switchCallbacks_->size()).c_str());
449     return ERRCODE_SUCCESS;
450 }
451 
SendGnssRequest(int type,MessageParcel & data,MessageParcel & reply)452 LocationErrCode LocatorAbility::SendGnssRequest(int type, MessageParcel &data, MessageParcel &reply)
453 {
454     auto remoteObject = proxyMap_->find(GNSS_ABILITY);
455     if (remoteObject == proxyMap_->end()) {
456         return ERRCODE_SERVICE_UNAVAILABLE;
457     }
458     auto obj = remoteObject->second;
459     if (obj == nullptr) {
460         return ERRCODE_SERVICE_UNAVAILABLE;
461     }
462     MessageOption option;
463     obj->SendRequest(type, data, reply, option);
464     return LocationErrCode(reply.ReadInt32());
465 }
466 
RegisterGnssStatusCallback(const sptr<IRemoteObject> & callback,pid_t uid)467 LocationErrCode LocatorAbility::RegisterGnssStatusCallback(const sptr<IRemoteObject>& callback, pid_t uid)
468 {
469     LBSLOGD(LOCATOR, "uid is: %{public}d", uid);
470     MessageParcel dataToStub;
471     MessageParcel replyToStub;
472     if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
473         return ERRCODE_SERVICE_UNAVAILABLE;
474     }
475     dataToStub.WriteRemoteObject(callback);
476     return SendGnssRequest(REG_GNSS_STATUS, dataToStub, replyToStub);
477 }
478 
UnregisterGnssStatusCallback(const sptr<IRemoteObject> & callback)479 LocationErrCode LocatorAbility::UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback)
480 {
481     MessageParcel dataToStub;
482     MessageParcel replyToStub;
483     if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
484         return ERRCODE_SERVICE_UNAVAILABLE;
485     }
486     dataToStub.WriteRemoteObject(callback);
487     return SendGnssRequest(UNREG_GNSS_STATUS, dataToStub, replyToStub);
488 }
489 
RegisterNmeaMessageCallback(const sptr<IRemoteObject> & callback,pid_t uid)490 LocationErrCode LocatorAbility::RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, pid_t uid)
491 {
492     MessageParcel dataToStub;
493     MessageParcel replyToStub;
494     if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
495         return ERRCODE_SERVICE_UNAVAILABLE;
496     }
497     dataToStub.WriteRemoteObject(callback);
498     return SendGnssRequest(REG_NMEA, dataToStub, replyToStub);
499 }
500 
UnregisterNmeaMessageCallback(const sptr<IRemoteObject> & callback)501 LocationErrCode LocatorAbility::UnregisterNmeaMessageCallback(const sptr<IRemoteObject>& callback)
502 {
503     MessageParcel dataToStub;
504     MessageParcel replyToStub;
505     if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
506         return ERRCODE_SERVICE_UNAVAILABLE;
507     }
508     dataToStub.WriteRemoteObject(callback);
509     return SendGnssRequest(UNREG_NMEA, dataToStub, replyToStub);
510 }
511 
RegisterCountryCodeCallback(const sptr<IRemoteObject> & callback,pid_t uid)512 LocationErrCode LocatorAbility::RegisterCountryCodeCallback(const sptr<IRemoteObject>& callback, pid_t uid)
513 {
514     if (countryCodeManager_ == nullptr) {
515         LBSLOGE(LOCATOR, "RegisterCountryCodeCallback countryCodeManager_ is nullptr");
516         return ERRCODE_SERVICE_UNAVAILABLE;
517     }
518     countryCodeManager_->RegisterCountryCodeCallback(callback, uid);
519     return ERRCODE_SUCCESS;
520 }
521 
UnregisterCountryCodeCallback(const sptr<IRemoteObject> & callback)522 LocationErrCode LocatorAbility::UnregisterCountryCodeCallback(const sptr<IRemoteObject>& callback)
523 {
524     if (countryCodeManager_ == nullptr) {
525         LBSLOGE(LOCATOR, "UnregisterCountryCodeCallback countryCodeManager_ is nullptr");
526         return ERRCODE_SERVICE_UNAVAILABLE;
527     }
528     countryCodeManager_->UnregisterCountryCodeCallback(callback);
529     return ERRCODE_SUCCESS;
530 }
531 
RegisterCachedLocationCallback(std::unique_ptr<CachedGnssLocationsRequest> & request,sptr<ICachedLocationsCallback> & callback,std::string bundleName)532 LocationErrCode LocatorAbility::RegisterCachedLocationCallback(std::unique_ptr<CachedGnssLocationsRequest>& request,
533     sptr<ICachedLocationsCallback>& callback, std::string bundleName)
534 {
535     MessageParcel dataToStub;
536     MessageParcel replyToStub;
537     if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
538         return ERRCODE_SERVICE_UNAVAILABLE;
539     }
540     dataToStub.WriteInt32(request->reportingPeriodSec);
541     dataToStub.WriteBool(request->wakeUpCacheQueueFull);
542     dataToStub.WriteRemoteObject(callback->AsObject());
543     dataToStub.WriteString16(Str8ToStr16(bundleName));
544     return SendGnssRequest(REG_CACHED, dataToStub, replyToStub);
545 }
546 
UnregisterCachedLocationCallback(sptr<ICachedLocationsCallback> & callback)547 LocationErrCode LocatorAbility::UnregisterCachedLocationCallback(sptr<ICachedLocationsCallback>& callback)
548 {
549     MessageParcel dataToStub;
550     MessageParcel replyToStub;
551     if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
552         return ERRCODE_SERVICE_UNAVAILABLE;
553     }
554     dataToStub.WriteRemoteObject(callback->AsObject());
555     return SendGnssRequest(UNREG_CACHED, dataToStub, replyToStub);
556 }
557 
GetCachedGnssLocationsSize(int & size)558 LocationErrCode LocatorAbility::GetCachedGnssLocationsSize(int& size)
559 {
560     MessageParcel dataToStub;
561     MessageParcel replyToStub;
562     if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
563         return ERRCODE_SERVICE_UNAVAILABLE;
564     }
565     LocationErrCode errorCode = SendGnssRequest(GET_CACHED_SIZE, dataToStub, replyToStub);
566     if (errorCode == ERRCODE_SUCCESS) {
567         size = replyToStub.ReadInt32();
568     }
569     return errorCode;
570 }
571 
FlushCachedGnssLocations()572 LocationErrCode LocatorAbility::FlushCachedGnssLocations()
573 {
574     MessageParcel dataToStub;
575     MessageParcel replyToStub;
576     if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
577         return ERRCODE_SERVICE_UNAVAILABLE;
578     }
579     return SendGnssRequest(FLUSH_CACHED, dataToStub, replyToStub);
580 }
581 
SendCommand(std::unique_ptr<LocationCommand> & commands)582 LocationErrCode LocatorAbility::SendCommand(std::unique_ptr<LocationCommand>& commands)
583 {
584     MessageParcel dataToStub;
585     MessageParcel replyToStub;
586     if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
587         return ERRCODE_SERVICE_UNAVAILABLE;
588     }
589     dataToStub.WriteInt32(commands->scenario);
590     dataToStub.WriteString16(Str8ToStr16(commands->command));
591     return SendGnssRequest(SEND_COMMANDS, dataToStub, replyToStub);
592 }
593 
AddFence(std::unique_ptr<GeofenceRequest> & request)594 LocationErrCode LocatorAbility::AddFence(std::unique_ptr<GeofenceRequest>& request)
595 {
596     MessageParcel dataToStub;
597     MessageParcel replyToStub;
598     if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
599         return ERRCODE_SERVICE_UNAVAILABLE;
600     }
601     dataToStub.WriteInt32(request->scenario);
602     dataToStub.WriteDouble(request->geofence.latitude);
603     dataToStub.WriteDouble(request->geofence.longitude);
604     dataToStub.WriteDouble(request->geofence.radius);
605     dataToStub.WriteDouble(request->geofence.expiration);
606     return SendGnssRequest(ADD_FENCE_INFO, dataToStub, replyToStub);
607 }
608 
RemoveFence(std::unique_ptr<GeofenceRequest> & request)609 LocationErrCode LocatorAbility::RemoveFence(std::unique_ptr<GeofenceRequest>& request)
610 {
611     MessageParcel dataToStub;
612     MessageParcel replyToStub;
613     if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) {
614         return ERRCODE_SERVICE_UNAVAILABLE;
615     }
616     dataToStub.WriteInt32(request->scenario);
617     dataToStub.WriteDouble(request->geofence.latitude);
618     dataToStub.WriteDouble(request->geofence.longitude);
619     dataToStub.WriteDouble(request->geofence.radius);
620     dataToStub.WriteDouble(request->geofence.expiration);
621     return SendGnssRequest(REMOVE_FENCE_INFO, dataToStub, replyToStub);
622 }
623 
GetIsoCountryCode(std::shared_ptr<CountryCode> & countryCode)624 LocationErrCode LocatorAbility::GetIsoCountryCode(std::shared_ptr<CountryCode>& countryCode)
625 {
626     if (countryCodeManager_ == nullptr) {
627         countryCode = nullptr;
628         LBSLOGE(LOCATOR, "GetIsoCountryCode countryCodeManager_ is nullptr");
629         return ERRCODE_SERVICE_UNAVAILABLE;
630     }
631     countryCode = countryCodeManager_->GetIsoCountryCode();
632     return ERRCODE_SUCCESS;
633 }
634 
SendLocationMockMsgToGnssSa(const sptr<IRemoteObject> obj,const int timeInterval,const std::vector<std::shared_ptr<Location>> & location,int msgId)635 LocationErrCode LocatorAbility::SendLocationMockMsgToGnssSa(const sptr<IRemoteObject> obj,
636     const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId)
637 {
638     if (obj == nullptr) {
639         LBSLOGE(LOCATOR, "SendLocationMockMsgToGnssSa obj is nullptr");
640         return ERRCODE_SERVICE_UNAVAILABLE;
641     }
642     std::unique_ptr<GnssAbilityProxy> gnssProxy = std::make_unique<GnssAbilityProxy>(obj);
643     if (msgId == ENABLE_LOCATION_MOCK) {
644         return gnssProxy->EnableMock();
645     } else if (msgId == DISABLE_LOCATION_MOCK) {
646         return gnssProxy->DisableMock();
647     } else if (msgId == SET_MOCKED_LOCATIONS) {
648         return gnssProxy->SetMocked(timeInterval, location);
649     }
650     return ERRCODE_NOT_SUPPORTED;
651 }
652 
SendLocationMockMsgToNetworkSa(const sptr<IRemoteObject> obj,const int timeInterval,const std::vector<std::shared_ptr<Location>> & location,int msgId)653 LocationErrCode LocatorAbility::SendLocationMockMsgToNetworkSa(const sptr<IRemoteObject> obj,
654     const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId)
655 {
656     if (obj == nullptr) {
657         LBSLOGE(LOCATOR, "SendLocationMockMsgToNetworkSa obj is nullptr");
658         return ERRCODE_SERVICE_UNAVAILABLE;
659     }
660     std::unique_ptr<NetworkAbilityProxy> networkProxy =
661         std::make_unique<NetworkAbilityProxy>(obj);
662     if (msgId == ENABLE_LOCATION_MOCK) {
663         return networkProxy->EnableMock();
664     } else if (msgId == DISABLE_LOCATION_MOCK) {
665         return networkProxy->DisableMock();
666     } else if (msgId == SET_MOCKED_LOCATIONS) {
667         return networkProxy->SetMocked(timeInterval, location);
668     }
669     return ERRCODE_NOT_SUPPORTED;
670 }
671 
SendLocationMockMsgToPassiveSa(const sptr<IRemoteObject> obj,const int timeInterval,const std::vector<std::shared_ptr<Location>> & location,int msgId)672 LocationErrCode LocatorAbility::SendLocationMockMsgToPassiveSa(const sptr<IRemoteObject> obj,
673     const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId)
674 {
675     if (obj == nullptr) {
676         LBSLOGE(LOCATOR, "SendLocationMockMsgToNetworkSa obj is nullptr");
677         return ERRCODE_SERVICE_UNAVAILABLE;
678     }
679     std::unique_ptr<PassiveAbilityProxy> passiveProxy =
680         std::make_unique<PassiveAbilityProxy>(obj);
681     if (msgId == ENABLE_LOCATION_MOCK) {
682         return passiveProxy->EnableMock();
683     } else if (msgId == DISABLE_LOCATION_MOCK) {
684         return passiveProxy->DisableMock();
685     } else if (msgId == SET_MOCKED_LOCATIONS) {
686         return passiveProxy->SetMocked(timeInterval, location);
687     }
688     return ERRCODE_NOT_SUPPORTED;
689 }
690 
ProcessLocationMockMsg(const int timeInterval,const std::vector<std::shared_ptr<Location>> & location,int msgId)691 LocationErrCode LocatorAbility::ProcessLocationMockMsg(
692     const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId)
693 {
694     for (auto iter = proxyMap_->begin(); iter != proxyMap_->end(); iter++) {
695         auto obj = iter->second;
696         if (iter->first == GNSS_ABILITY) {
697             SendLocationMockMsgToGnssSa(obj, timeInterval, location, msgId);
698         } else if (iter->first == NETWORK_ABILITY) {
699             SendLocationMockMsgToNetworkSa(obj, timeInterval, location, msgId);
700         } else if (iter->first == PASSIVE_ABILITY) {
701             SendLocationMockMsgToPassiveSa(obj, timeInterval, location, msgId);
702         }
703     }
704     return ERRCODE_SUCCESS;
705 }
706 
EnableLocationMock()707 LocationErrCode LocatorAbility::EnableLocationMock()
708 {
709     int timeInterval = 0;
710     std::vector<std::shared_ptr<Location>> location;
711     return ProcessLocationMockMsg(timeInterval, location, ENABLE_LOCATION_MOCK);
712 }
713 
DisableLocationMock()714 LocationErrCode LocatorAbility::DisableLocationMock()
715 {
716     int timeInterval = 0;
717     std::vector<std::shared_ptr<Location>> location;
718     return ProcessLocationMockMsg(timeInterval, location, DISABLE_LOCATION_MOCK);
719 }
720 
SetMockedLocations(const int timeInterval,const std::vector<std::shared_ptr<Location>> & location)721 LocationErrCode LocatorAbility::SetMockedLocations(
722     const int timeInterval, const std::vector<std::shared_ptr<Location>> &location)
723 {
724     return ProcessLocationMockMsg(timeInterval, location, SET_MOCKED_LOCATIONS);
725 }
726 
StartLocating(std::unique_ptr<RequestConfig> & requestConfig,sptr<ILocatorCallback> & callback,AppIdentity & identity)727 LocationErrCode LocatorAbility::StartLocating(std::unique_ptr<RequestConfig>& requestConfig,
728     sptr<ILocatorCallback>& callback, AppIdentity &identity)
729 {
730     if (isEnabled_ == DISABLED) {
731         ReportErrorStatus(callback, ERROR_SWITCH_UNOPEN);
732     }
733     if (!CheckSaValid()) {
734         InitSaAbility();
735     }
736     // update offset before add request
737     if (reportManager_ == nullptr || requestManager_ == nullptr) {
738         return ERRCODE_SERVICE_UNAVAILABLE;
739     }
740     reportManager_->UpdateRandom();
741     // generate request object according to input params
742     std::shared_ptr<Request> request = std::make_shared<Request>();
743     request->SetUid(identity.GetUid());
744     request->SetPid(identity.GetPid());
745     request->SetTokenId(identity.GetTokenId());
746     request->SetFirstTokenId(identity.GetFirstTokenId());
747     request->SetPackageName(identity.GetBundleName());
748     request->SetRequestConfig(*requestConfig);
749     request->SetLocatorCallBack(callback);
750     request->SetUuid(std::to_string(CommonUtils::IntRandom(MIN_INT_RANDOM, MAX_INT_RANDOM)));
751     LBSLOGI(LOCATOR, "start locating");
752     requestManager_->HandleStartLocating(request);
753     ReportLocationStatus(callback, SESSION_START);
754     return ERRCODE_SUCCESS;
755 }
756 
StopLocating(sptr<ILocatorCallback> & callback)757 LocationErrCode LocatorAbility::StopLocating(sptr<ILocatorCallback>& callback)
758 {
759     LBSLOGI(LOCATOR, "stop locating");
760     if (requestManager_ == nullptr) {
761         return ERRCODE_SERVICE_UNAVAILABLE;
762     }
763     requestManager_->HandleStopLocating(callback);
764     ReportLocationStatus(callback, SESSION_STOP);
765     return ERRCODE_SUCCESS;
766 }
767 
GetCacheLocation(std::unique_ptr<Location> & loc,AppIdentity & identity)768 LocationErrCode LocatorAbility::GetCacheLocation(std::unique_ptr<Location>& loc, AppIdentity &identity)
769 {
770     auto lastLocation = reportManager_->GetLastLocation();
771     loc = reportManager_->GetPermittedLocation(identity.GetTokenId(),
772         identity.GetFirstTokenId(), lastLocation);
773     if (loc == nullptr) {
774         return ERRCODE_LOCATING_FAIL;
775     }
776     if (fabs(loc->GetLatitude() - 0.0) > PRECISION
777         && fabs(loc->GetLongitude() - 0.0) > PRECISION) {
778         return ERRCODE_SUCCESS;
779     }
780     return ERRCODE_LOCATING_FAIL;
781 }
782 
ReportLocation(const std::unique_ptr<Location> & location,std::string abilityName)783 LocationErrCode LocatorAbility::ReportLocation(const std::unique_ptr<Location>& location, std::string abilityName)
784 {
785     if (requests_ == nullptr) {
786         return ERRCODE_SERVICE_UNAVAILABLE;
787     }
788     int state = DISABLED;
789     LocationErrCode errorCode = GetSwitchState(state);
790     if (errorCode != ERRCODE_SUCCESS) {
791         return errorCode;
792     }
793     if (state == DISABLED) {
794         LBSLOGE(LOCATOR, "location switch is off");
795         return ERRCODE_SWITCH_OFF;
796     }
797     LBSLOGI(LOCATOR, "start report location");
798     if (reportManager_->OnReportLocation(location, abilityName)) {
799         return ERRCODE_SUCCESS;
800     }
801     return ERRCODE_SERVICE_UNAVAILABLE;
802 }
803 
ReportLocationStatus(sptr<ILocatorCallback> & callback,int result)804 LocationErrCode LocatorAbility::ReportLocationStatus(sptr<ILocatorCallback>& callback, int result)
805 {
806     int state = DISABLED;
807     LocationErrCode errorCode = GetSwitchState(state);
808     if (errorCode != ERRCODE_SUCCESS) {
809         return errorCode;
810     }
811     if (state == DISABLED) {
812         LBSLOGE(LOCATOR, "location switch is off");
813         return ERRCODE_SWITCH_OFF;
814     }
815     if (reportManager_->ReportRemoteCallback(callback, ILocatorCallback::RECEIVE_LOCATION_STATUS_EVENT, result)) {
816         return ERRCODE_SUCCESS;
817     }
818     return ERRCODE_SERVICE_UNAVAILABLE;
819 }
820 
ReportErrorStatus(sptr<ILocatorCallback> & callback,int result)821 LocationErrCode LocatorAbility::ReportErrorStatus(sptr<ILocatorCallback>& callback, int result)
822 {
823     int state = DISABLED;
824     LocationErrCode errorCode = GetSwitchState(state);
825     if (errorCode != ERRCODE_SUCCESS) {
826         return errorCode;
827     }
828     if (state == DISABLED) {
829         LBSLOGE(LOCATOR, "location switch is off");
830         return ERRCODE_SWITCH_OFF;
831     }
832     if (reportManager_->ReportRemoteCallback(callback, ILocatorCallback::RECEIVE_ERROR_INFO_EVENT, result)) {
833         return ERRCODE_SUCCESS;
834     }
835     return ERRCODE_SERVICE_UNAVAILABLE;
836 }
837 
RegisterAction()838 void LocatorAbility::RegisterAction()
839 {
840     if (isActionRegistered) {
841         LBSLOGI(LOCATOR, "action has already registered");
842         return;
843     }
844     OHOS::EventFwk::MatchingSkills matchingSkills;
845     matchingSkills.AddEvent(MODE_CHANGED_EVENT);
846     OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
847     locatorEventSubscriber_ = std::make_shared<LocatorEventSubscriber>(subscriberInfo);
848 
849     bool result = OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(locatorEventSubscriber_);
850     if (!result) {
851         LBSLOGE(LOCATOR, "Failed to subscriber locator event, result = %{public}d", result);
852         isActionRegistered = false;
853     } else {
854         LBSLOGI(LOCATOR, "success to subscriber locator event, result = %{public}d", result);
855         isActionRegistered = true;
856     }
857 }
858 
IsGeoConvertAvailable(bool & isAvailable)859 LocationErrCode LocatorAbility::IsGeoConvertAvailable(bool &isAvailable)
860 {
861     MessageParcel dataParcel;
862     MessageParcel replyParcel;
863     if (!dataParcel.WriteInterfaceToken(GeoConvertProxy::GetDescriptor())) {
864         isAvailable = false;
865         return ERRCODE_SERVICE_UNAVAILABLE;
866     }
867     SendGeoRequest(GEO_IS_AVAILABLE, dataParcel, replyParcel);
868     LocationErrCode errorCode = LocationErrCode(replyParcel.ReadInt32());
869     if (errorCode == ERRCODE_SUCCESS) {
870         isAvailable = replyParcel.ReadBool();
871     } else {
872         isAvailable = false;
873     }
874     return errorCode;
875 }
876 
GetAddressByCoordinate(MessageParcel & data,MessageParcel & reply)877 void LocatorAbility::GetAddressByCoordinate(MessageParcel &data, MessageParcel &reply)
878 {
879     LBSLOGI(LOCATOR, "locator_ability GetAddressByCoordinate");
880     MessageParcel dataParcel;
881     if (!dataParcel.WriteInterfaceToken(GeoConvertProxy::GetDescriptor())) {
882         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
883         return;
884     }
885     dataParcel.WriteDouble(data.ReadDouble()); // latitude
886     dataParcel.WriteDouble(data.ReadDouble()); // longitude
887     dataParcel.WriteInt32(data.ReadInt32()); // maxItems
888     dataParcel.WriteInt32(data.ReadInt32()); // locale object size = 1
889     dataParcel.WriteString16(data.ReadString16()); // locale.getLanguage()
890     dataParcel.WriteString16(data.ReadString16()); // locale.getCountry()
891     dataParcel.WriteString16(data.ReadString16()); // locale.getVariant()
892     dataParcel.WriteString16(data.ReadString16()); // ""
893     SendGeoRequest(GET_FROM_COORDINATE, dataParcel, reply);
894 }
895 
GetAddressByLocationName(MessageParcel & data,MessageParcel & reply)896 void LocatorAbility::GetAddressByLocationName(MessageParcel &data, MessageParcel &reply)
897 {
898     LBSLOGI(LOCATOR, "locator_ability GetAddressByLocationName");
899     MessageParcel dataParcel;
900     if (!dataParcel.WriteInterfaceToken(GeoConvertProxy::GetDescriptor())) {
901         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
902         return;
903     }
904     dataParcel.WriteString16(data.ReadString16()); // description
905     dataParcel.WriteDouble(data.ReadDouble()); // minLatitude
906     dataParcel.WriteDouble(data.ReadDouble()); // minLongitude
907     dataParcel.WriteDouble(data.ReadDouble()); // maxLatitude
908     dataParcel.WriteDouble(data.ReadDouble()); // maxLongitude
909     dataParcel.WriteInt32(data.ReadInt32()); // maxreplyItems
910     dataParcel.WriteInt32(data.ReadInt32()); // locale object size = 1
911     dataParcel.WriteString16(data.ReadString16()); // locale.getLanguage()
912     dataParcel.WriteString16(data.ReadString16()); // locale.getCountry()
913     dataParcel.WriteString16(data.ReadString16()); // locale.getVariant()
914     dataParcel.WriteString16(data.ReadString16()); // ""
915     SendGeoRequest(GET_FROM_LOCATION_NAME, dataParcel, reply);
916 }
917 
SendGeoRequest(int type,MessageParcel & data,MessageParcel & reply)918 LocationErrCode LocatorAbility::SendGeoRequest(int type, MessageParcel &data, MessageParcel &reply)
919 {
920     sptr<IRemoteObject> remoteObject = CommonUtils::GetRemoteObject(LOCATION_GEO_CONVERT_SA_ID,
921         CommonUtils::InitDeviceId());
922     if (remoteObject == nullptr) {
923         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
924         return ERRCODE_SERVICE_UNAVAILABLE;
925     }
926     MessageOption option;
927     remoteObject->SendRequest(type, data, reply, option);
928     return ERRCODE_SUCCESS;
929 }
930 
EnableReverseGeocodingMock()931 LocationErrCode LocatorAbility::EnableReverseGeocodingMock()
932 {
933     MessageParcel dataParcel;
934     MessageParcel replyParcel;
935     if (!dataParcel.WriteInterfaceToken(GeoConvertProxy::GetDescriptor())) {
936         return ERRCODE_SERVICE_UNAVAILABLE;
937     }
938     SendGeoRequest(ENABLE_REVERSE_GEOCODE_MOCK, dataParcel, replyParcel);
939     return LocationErrCode(replyParcel.ReadInt32());
940 }
941 
DisableReverseGeocodingMock()942 LocationErrCode LocatorAbility::DisableReverseGeocodingMock()
943 {
944     MessageParcel dataParcel;
945     MessageParcel replyParcel;
946     if (!dataParcel.WriteInterfaceToken(GeoConvertProxy::GetDescriptor())) {
947         return ERRCODE_SERVICE_UNAVAILABLE;
948     }
949     SendGeoRequest(DISABLE_REVERSE_GEOCODE_MOCK, dataParcel, replyParcel);
950     return LocationErrCode(replyParcel.ReadInt32());
951 }
952 
SetReverseGeocodingMockInfo(std::vector<std::shared_ptr<GeocodingMockInfo>> & mockInfo)953 LocationErrCode LocatorAbility::SetReverseGeocodingMockInfo(std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo)
954 {
955     MessageParcel dataParcel;
956     MessageParcel replyParcel;
957     if (!dataParcel.WriteInterfaceToken(GeoConvertProxy::GetDescriptor())) {
958         return ERRCODE_SERVICE_UNAVAILABLE;
959     }
960     dataParcel.WriteInt32(mockInfo.size());
961     for (size_t i = 0; i < mockInfo.size(); i++) {
962         mockInfo[i]->Marshalling(dataParcel);
963     }
964     SendGeoRequest(SET_REVERSE_GEOCODE_MOCKINFO, dataParcel, replyParcel);
965     return LocationErrCode(replyParcel.ReadInt32());
966 }
967 
ProxyUidForFreeze(int32_t uid,bool isProxy)968 LocationErrCode LocatorAbility::ProxyUidForFreeze(int32_t uid, bool isProxy)
969 {
970     LBSLOGI(LOCATOR, "Start locator proxy, uid: %{public}d, isProxy: %{public}d", uid, isProxy);
971     std::lock_guard<std::mutex> lock(proxyMutex_);
972     if (isProxy) {
973         proxyUids_.insert(uid);
974     } else {
975         proxyUids_.erase(uid);
976     }
977     return ERRCODE_SUCCESS;
978 }
979 
ResetAllProxy()980 LocationErrCode LocatorAbility::ResetAllProxy()
981 {
982     LBSLOGI(LOCATOR, "Start locator ResetAllProxy");
983     std::lock_guard<std::mutex> lock(proxyMutex_);
984     proxyUids_.clear();
985     return ERRCODE_SUCCESS;
986 }
987 
IsProxyUid(int32_t uid)988 bool LocatorAbility::IsProxyUid(int32_t uid)
989 {
990     std::lock_guard<std::mutex> lock(proxyMutex_);
991     return proxyUids_.find(uid) != proxyUids_.end();
992 }
993 
RegisterPermissionCallback(const uint32_t callingTokenId,const std::vector<std::string> & permissionNameList)994 void LocatorAbility::RegisterPermissionCallback(const uint32_t callingTokenId,
995     const std::vector<std::string>& permissionNameList)
996 {
997     if (permissionMap_ == nullptr) {
998         LBSLOGE(LOCATOR, "permissionMap is null.");
999         return;
1000     }
1001     PermStateChangeScope scopeInfo;
1002     scopeInfo.permList = permissionNameList;
1003     scopeInfo.tokenIDs = {callingTokenId};
1004     auto callbackPtr = std::make_shared<PermissionStatusChangeCb>(scopeInfo);
1005     std::lock_guard<std::mutex> lock(permissionMutex_);
1006     permissionMap_->erase(callingTokenId);
1007     permissionMap_->insert(std::make_pair(callingTokenId, callbackPtr));
1008     LBSLOGD(LOCATOR, "after tokenId:%{public}d register, permission callback size:%{public}s",
1009         callingTokenId, std::to_string(permissionMap_->size()).c_str());
1010     int32_t res = AccessTokenKit::RegisterPermStateChangeCallback(callbackPtr);
1011     if (res != SUCCESS) {
1012         LBSLOGE(LOCATOR, "RegisterPermStateChangeCallback failed.");
1013     }
1014 }
1015 
UnregisterPermissionCallback(const uint32_t callingTokenId)1016 void LocatorAbility::UnregisterPermissionCallback(const uint32_t callingTokenId)
1017 {
1018     if (permissionMap_ == nullptr) {
1019         LBSLOGE(LOCATOR, "permissionMap is null.");
1020         return;
1021     }
1022     std::lock_guard<std::mutex> lock(permissionMutex_);
1023     auto iter = permissionMap_->find(callingTokenId);
1024     if (iter != permissionMap_->end()) {
1025         auto callbackPtr = iter->second;
1026         int32_t res = AccessTokenKit::UnRegisterPermStateChangeCallback(callbackPtr);
1027         if (res != SUCCESS) {
1028             LBSLOGE(LOCATOR, "UnRegisterPermStateChangeCallback failed.");
1029         }
1030     }
1031     permissionMap_->erase(callingTokenId);
1032     LBSLOGD(LOCATOR, "after tokenId:%{public}d unregister, permission callback size:%{public}s",
1033         callingTokenId, std::to_string(permissionMap_->size()).c_str());
1034 }
1035 } // namespace Location
1036 } // namespace OHOS
1037