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 "geo_coding_mock_info.h" 29 #include "i_switch_callback.h" 30 #include "i_cached_locations_callback.h" 31 #include "locator_event_subscriber.h" 32 #include "locator_skeleton.h" 33 #include "permission_status_change_cb.h" 34 #include "request.h" 35 #include "request_manager.h" 36 #include "report_manager.h" 37 38 namespace OHOS { 39 namespace Location { 40 class LocatorHandler : public AppExecFwk::EventHandler { 41 public: 42 using LocatorEventHandle = void (LocatorHandler::*)( 43 const AppExecFwk::InnerEvent::Pointer& event); 44 using LocatorEventHandleMap = std::map<int, LocatorEventHandle>; 45 explicit LocatorHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 46 ~LocatorHandler() override; 47 void InitLocatorHandlerEventMap(); 48 private: 49 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override; 50 void UpdateSaEvent(const AppExecFwk::InnerEvent::Pointer& event); 51 void InitRequestManagerEvent(const AppExecFwk::InnerEvent::Pointer& event); 52 void ApplyRequirementsEvent(const AppExecFwk::InnerEvent::Pointer& event); 53 void RetryRegisterActionEvent(const AppExecFwk::InnerEvent::Pointer& event); 54 void ReportLocationMessageEvent(const AppExecFwk::InnerEvent::Pointer& event); 55 void SendSwitchStateToHifenceEvent(const AppExecFwk::InnerEvent::Pointer& event); 56 void UnloadSaEvent(const AppExecFwk::InnerEvent::Pointer& event); 57 void StartLocatingEvent(const AppExecFwk::InnerEvent::Pointer& event); 58 void StopLocatingEvent(const AppExecFwk::InnerEvent::Pointer& event); 59 LocatorEventHandleMap locatorHandlerEventMap_; 60 }; 61 62 class LocatorAbility : public SystemAbility, public LocatorAbilityStub, DelayedSingleton<LocatorAbility> { 63 DECLEAR_SYSTEM_ABILITY(LocatorAbility); 64 65 public: 66 DISALLOW_COPY_AND_MOVE(LocatorAbility); 67 LocatorAbility(); 68 ~LocatorAbility() override; 69 void OnStart() override; 70 void OnStop() override; 71 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 72 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; QueryServiceState()73 ServiceRunningState QueryServiceState() const 74 { 75 return state_; 76 } 77 void InitSaAbility(); 78 void InitRequestManagerMap(); 79 80 LocationErrCode UpdateSaAbility(); 81 LocationErrCode GetSwitchState(int& state); 82 LocationErrCode EnableAbility(bool isEnabled); 83 LocationErrCode RegisterSwitchCallback(const sptr<IRemoteObject>& callback, pid_t uid); 84 LocationErrCode UnregisterSwitchCallback(const sptr<IRemoteObject>& callback); 85 #ifdef FEATURE_GNSS_SUPPORT 86 LocationErrCode RegisterGnssStatusCallback(const sptr<IRemoteObject>& callback, pid_t uid); 87 LocationErrCode UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback); 88 LocationErrCode RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, pid_t uid); 89 LocationErrCode UnregisterNmeaMessageCallback(const sptr<IRemoteObject>& callback); 90 LocationErrCode RegisterCachedLocationCallback(std::unique_ptr<CachedGnssLocationsRequest>& request, 91 sptr<ICachedLocationsCallback>& callback, std::string bundleName); 92 LocationErrCode UnregisterCachedLocationCallback(sptr<ICachedLocationsCallback>& callback); 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 #endif 99 LocationErrCode StartLocating(std::unique_ptr<RequestConfig>& requestConfig, 100 sptr<ILocatorCallback>& callback, AppIdentity &identity); 101 LocationErrCode StopLocating(sptr<ILocatorCallback>& callback); 102 LocationErrCode GetCacheLocation(std::unique_ptr<Location>& loc, AppIdentity &identity); 103 #ifdef FEATURE_GEOCODE_SUPPORT 104 LocationErrCode IsGeoConvertAvailable(bool &isAvailable); 105 void GetAddressByCoordinate(MessageParcel &data, MessageParcel &reply, std::string bundleName); 106 void GetAddressByLocationName(MessageParcel &data, MessageParcel &reply, std::string bundleName); 107 LocationErrCode EnableReverseGeocodingMock(); 108 LocationErrCode DisableReverseGeocodingMock(); 109 LocationErrCode SetReverseGeocodingMockInfo(std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo); 110 #endif 111 LocationErrCode IsLocationPrivacyConfirmed(const int type, bool& isConfirmed); 112 LocationErrCode SetLocationPrivacyConfirmStatus(const int type, bool isConfirmed); 113 LocationErrCode EnableLocationMock(); 114 LocationErrCode DisableLocationMock(); 115 LocationErrCode SetMockedLocations( 116 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location); 117 LocationErrCode ReportLocation(const std::unique_ptr<Location>& location, std::string abilityName); 118 LocationErrCode ReportLocationStatus(sptr<ILocatorCallback>& callback, int result); 119 LocationErrCode ReportErrorStatus(sptr<ILocatorCallback>& callback, int result); 120 LocationErrCode ProcessLocationMockMsg( 121 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId); 122 #ifdef FEATURE_GNSS_SUPPORT 123 LocationErrCode SendLocationMockMsgToGnssSa(const sptr<IRemoteObject> obj, 124 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId); 125 #endif 126 #ifdef FEATURE_NETWORK_SUPPORT 127 LocationErrCode SendLocationMockMsgToNetworkSa(const sptr<IRemoteObject> obj, 128 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId); 129 #endif 130 #ifdef FEATURE_PASSIVE_SUPPORT 131 LocationErrCode SendLocationMockMsgToPassiveSa(const sptr<IRemoteObject> obj, 132 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId); 133 #endif 134 LocationErrCode RegisterWifiScanInfoCallback(const sptr<IRemoteObject>& callback, pid_t uid); 135 LocationErrCode UnregisterWifiScanInfoCallback(const sptr<IRemoteObject>& callback); 136 LocationErrCode RegisterBluetoothScanInfoCallback(const sptr<IRemoteObject>& callback, pid_t uid); 137 LocationErrCode UnregisterBluetoothScanInfoCallback(const sptr<IRemoteObject>& callback); 138 LocationErrCode RegisterBleScanInfoCallback(const sptr<IRemoteObject>& callback, pid_t uid); 139 LocationErrCode UnregisterBleScanInfoCallback(const sptr<IRemoteObject>& callback); 140 141 std::shared_ptr<std::map<std::string, std::list<std::shared_ptr<Request>>>> GetRequests(); 142 std::shared_ptr<std::map<sptr<IRemoteObject>, std::list<std::shared_ptr<Request>>>> GetReceivers(); 143 std::shared_ptr<std::map<std::string, sptr<IRemoteObject>>> GetProxyMap(); 144 void UpdateSaAbilityHandler(); 145 void ApplyRequests(int delay); 146 void RegisterAction(); 147 LocationErrCode ProxyUidForFreeze(int32_t uid, bool isProxy); 148 LocationErrCode ResetAllProxy(); 149 bool IsProxyUid(int32_t uid); 150 int GetActiveRequestNum(); 151 void RegisterPermissionCallback(const uint32_t callingTokenId, const std::vector<std::string>& permissionNameList); 152 void UnregisterPermissionCallback(const uint32_t callingTokenId); 153 void RemoveUnloadTask(uint32_t code); 154 void PostUnloadTask(uint32_t code); 155 std::set<int32_t> GetProxyUid(); 156 157 private: 158 bool Init(); 159 bool CheckSaValid(); 160 #ifdef FEATURE_GEOCODE_SUPPORT 161 LocationErrCode SendGeoRequest(int type, MessageParcel &data, MessageParcel &reply); 162 #endif 163 #ifdef FEATURE_GNSS_SUPPORT 164 LocationErrCode SendGnssRequest(int type, MessageParcel &data, MessageParcel &reply); 165 #endif 166 void UpdateProxyMap(); 167 bool CheckIfLocatorConnecting(); 168 void UpdateLoadedSaMap(); 169 bool NeedReportCacheLocation(const std::shared_ptr<Request>& request, sptr<ILocatorCallback>& callback); 170 void HandleStartLocating(const std::shared_ptr<Request>& request, sptr<ILocatorCallback>& callback); 171 bool IsCacheVaildScenario(const sptr<RequestConfig>& requestConfig); 172 void SendSwitchState(const int state); 173 std::shared_ptr<Request> InitRequest(std::unique_ptr<RequestConfig>& requestConfig, 174 sptr<ILocatorCallback>& callback, AppIdentity &identity); 175 176 bool registerToAbility_ = false; 177 bool isActionRegistered = false; 178 std::string deviceId_; 179 ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START; 180 std::shared_ptr<LocatorEventSubscriber> locatorEventSubscriber_; 181 std::mutex switchMutex_; 182 std::mutex requestsMutex_; 183 std::mutex receiversMutex_; 184 std::mutex proxyMapMutex_; 185 std::mutex permissionMapMutex_; 186 std::mutex loadedSaMapMutex_; 187 std::unique_ptr<std::map<pid_t, sptr<ISwitchCallback>>> switchCallbacks_; 188 std::shared_ptr<std::map<std::string, std::list<std::shared_ptr<Request>>>> requests_; 189 std::shared_ptr<std::map<sptr<IRemoteObject>, std::list<std::shared_ptr<Request>>>> receivers_; 190 std::shared_ptr<std::map<std::string, sptr<IRemoteObject>>> proxyMap_; 191 std::shared_ptr<std::map<std::string, sptr<IRemoteObject>>> loadedSaMap_; 192 std::shared_ptr<std::map<uint32_t, std::shared_ptr<PermissionStatusChangeCb>>> permissionMap_; 193 std::shared_ptr<LocatorHandler> locatorHandler_; 194 std::shared_ptr<RequestManager> requestManager_; 195 std::shared_ptr<ReportManager> reportManager_; 196 std::mutex proxyUidsMutex_; 197 std::set<int32_t> proxyUids_; 198 }; 199 200 class LocationMessage { 201 public: 202 void SetAbilityName(std::string abilityName); 203 std::string GetAbilityName(); 204 void SetLocation(const std::unique_ptr<Location>& location); 205 std::unique_ptr<Location> GetLocation(); 206 private: 207 std::string abilityName_; 208 std::unique_ptr<Location> location_; 209 }; 210 211 class LocatorCallbackMessage { 212 public: 213 void SetCallback(const sptr<ILocatorCallback>& callback); 214 sptr<ILocatorCallback> GetCallback(); 215 private: 216 std::string abilityName_; 217 sptr<ILocatorCallback> callback_; 218 }; 219 } // namespace Location 220 } // namespace OHOS 221 #endif // LOCATOR_ABILITY_H 222