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 #ifndef LOCATOR_ABILITY_H 17 #define LOCATOR_ABILITY_H 18 19 #include <map> 20 #include <mutex> 21 #include <singleton.h> 22 23 #include "event_handler.h" 24 #include "system_ability.h" 25 26 #include "app_identity.h" 27 #include "common_utils.h" 28 #include "country_code_manager.h" 29 #include "country_code.h" 30 #include "geo_coding_mock_info.h" 31 #include "i_switch_callback.h" 32 #include "i_cached_locations_callback.h" 33 #include "locator_event_subscriber.h" 34 #include "locator_skeleton.h" 35 #include "permission_status_change_cb.h" 36 #include "request.h" 37 #include "request_manager.h" 38 #include "report_manager.h" 39 40 namespace OHOS { 41 namespace Location { 42 class LocatorHandler : public AppExecFwk::EventHandler { 43 public: 44 explicit LocatorHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 45 ~LocatorHandler() override; 46 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override; 47 }; 48 49 class LocatorAbility : public SystemAbility, public LocatorAbilityStub, DelayedSingleton<LocatorAbility> { 50 DECLEAR_SYSTEM_ABILITY(LocatorAbility); 51 52 public: 53 DISALLOW_COPY_AND_MOVE(LocatorAbility); 54 LocatorAbility(); 55 ~LocatorAbility() override; 56 void OnStart() override; 57 void OnStop() override; 58 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 59 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; QueryServiceState()60 ServiceRunningState QueryServiceState() const 61 { 62 return state_; 63 } 64 void InitSaAbility(); 65 void InitRequestManagerMap(); 66 67 LocationErrCode UpdateSaAbility(); 68 LocationErrCode GetSwitchState(int& state); 69 LocationErrCode EnableAbility(bool isEnabled); 70 LocationErrCode RegisterSwitchCallback(const sptr<IRemoteObject>& callback, pid_t uid); 71 LocationErrCode UnregisterSwitchCallback(const sptr<IRemoteObject>& callback); 72 LocationErrCode RegisterGnssStatusCallback(const sptr<IRemoteObject>& callback, pid_t uid); 73 LocationErrCode UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback); 74 LocationErrCode RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, pid_t uid); 75 LocationErrCode UnregisterNmeaMessageCallback(const sptr<IRemoteObject>& callback); 76 LocationErrCode RegisterCountryCodeCallback(const sptr<IRemoteObject>& callback, pid_t uid); 77 LocationErrCode UnregisterCountryCodeCallback(const sptr<IRemoteObject>& callback); 78 LocationErrCode StartLocating(std::unique_ptr<RequestConfig>& requestConfig, 79 sptr<ILocatorCallback>& callback, AppIdentity &identity); 80 LocationErrCode StopLocating(sptr<ILocatorCallback>& callback); 81 LocationErrCode GetCacheLocation(std::unique_ptr<Location>& loc, AppIdentity &identity); 82 LocationErrCode IsGeoConvertAvailable(bool &isAvailable); 83 void GetAddressByCoordinate(MessageParcel &data, MessageParcel &reply); 84 void GetAddressByLocationName(MessageParcel &data, MessageParcel &reply); 85 86 LocationErrCode IsLocationPrivacyConfirmed(const int type, bool& isConfirmed); 87 LocationErrCode SetLocationPrivacyConfirmStatus(const int type, bool isConfirmed); 88 89 LocationErrCode RegisterCachedLocationCallback(std::unique_ptr<CachedGnssLocationsRequest>& request, 90 sptr<ICachedLocationsCallback>& callback, std::string bundleName); 91 LocationErrCode UnregisterCachedLocationCallback(sptr<ICachedLocationsCallback>& callback); 92 93 LocationErrCode GetCachedGnssLocationsSize(int& size); 94 LocationErrCode FlushCachedGnssLocations(); 95 LocationErrCode SendCommand(std::unique_ptr<LocationCommand>& commands); 96 LocationErrCode AddFence(std::unique_ptr<GeofenceRequest>& request); 97 LocationErrCode RemoveFence(std::unique_ptr<GeofenceRequest>& request); 98 LocationErrCode GetIsoCountryCode(std::shared_ptr<CountryCode>& country); 99 LocationErrCode EnableLocationMock(); 100 LocationErrCode DisableLocationMock(); 101 LocationErrCode SetMockedLocations( 102 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location); 103 104 LocationErrCode EnableReverseGeocodingMock(); 105 LocationErrCode DisableReverseGeocodingMock(); 106 LocationErrCode SetReverseGeocodingMockInfo(std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo); 107 108 LocationErrCode ReportLocation(const std::unique_ptr<Location>& location, std::string abilityName); 109 LocationErrCode ReportLocationStatus(sptr<ILocatorCallback>& callback, int result); 110 LocationErrCode ReportErrorStatus(sptr<ILocatorCallback>& callback, int result); 111 LocationErrCode ProcessLocationMockMsg( 112 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId); 113 LocationErrCode SendLocationMockMsgToGnssSa(const sptr<IRemoteObject> obj, 114 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId); 115 LocationErrCode SendLocationMockMsgToNetworkSa(const sptr<IRemoteObject> obj, 116 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId); 117 LocationErrCode SendLocationMockMsgToPassiveSa(const sptr<IRemoteObject> obj, 118 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId); 119 120 std::shared_ptr<std::map<std::string, std::list<std::shared_ptr<Request>>>> GetRequests(); 121 std::shared_ptr<std::map<sptr<IRemoteObject>, std::list<std::shared_ptr<Request>>>> GetReceivers(); 122 std::shared_ptr<std::map<std::string, sptr<IRemoteObject>>> GetProxyMap(); 123 void UpdateSaAbilityHandler(); 124 void ApplyRequests(); 125 void RegisterAction(); 126 LocationErrCode ProxyUidForFreeze(int32_t uid, bool isProxy); 127 LocationErrCode ResetAllProxy(); 128 bool IsProxyUid(int32_t uid); 129 int GetActiveRequestNum(); 130 void RegisterPermissionCallback(const uint32_t callingTokenId, const std::vector<std::string>& permissionNameList); 131 void UnregisterPermissionCallback(const uint32_t callingTokenId); 132 133 private: 134 bool Init(); 135 bool CheckSaValid(); 136 static int QuerySwitchState(); 137 LocationErrCode SendGeoRequest(int type, MessageParcel &data, MessageParcel &reply); 138 LocationErrCode SendGnssRequest(int type, MessageParcel &data, MessageParcel &reply); 139 140 bool registerToAbility_ = false; 141 bool isActionRegistered = false; 142 bool isEnabled_ = false; 143 std::string deviceId_; 144 ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START; 145 std::shared_ptr<LocatorEventSubscriber> locatorEventSubscriber_; 146 std::unique_ptr<std::map<pid_t, sptr<ISwitchCallback>>> switchCallbacks_; 147 std::shared_ptr<std::map<std::string, std::list<std::shared_ptr<Request>>>> requests_; 148 std::shared_ptr<std::map<sptr<IRemoteObject>, std::list<std::shared_ptr<Request>>>> receivers_; 149 std::shared_ptr<std::map<std::string, sptr<IRemoteObject>>> proxyMap_; 150 std::shared_ptr<std::map<uint32_t, std::shared_ptr<PermissionStatusChangeCb>>> permissionMap_; 151 std::shared_ptr<LocatorHandler> locatorHandler_; 152 std::shared_ptr<RequestManager> requestManager_; 153 std::shared_ptr<ReportManager> reportManager_; 154 std::shared_ptr<CountryCodeManager> countryCodeManager_ = nullptr; 155 156 std::mutex proxyMutex_; 157 std::mutex permissionMutex_; 158 std::mutex switchMutex_; 159 std::set<int32_t> proxyUids_; 160 }; 161 } // namespace Location 162 } // namespace OHOS 163 #endif // LOCATOR_ABILITY_H 164