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 explicit LocatorHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 43 ~LocatorHandler() override; 44 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override; 45 }; 46 47 class LocatorAbility : public SystemAbility, public LocatorAbilityStub, DelayedSingleton<LocatorAbility> { 48 DECLEAR_SYSTEM_ABILITY(LocatorAbility); 49 50 public: 51 DISALLOW_COPY_AND_MOVE(LocatorAbility); 52 LocatorAbility(); 53 ~LocatorAbility() override; 54 void OnStart() override; 55 void OnStop() override; 56 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 57 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; QueryServiceState()58 ServiceRunningState QueryServiceState() const 59 { 60 return state_; 61 } 62 void InitSaAbility(); 63 void InitRequestManagerMap(); 64 65 LocationErrCode UpdateSaAbility(); 66 LocationErrCode GetSwitchState(int& state); 67 LocationErrCode EnableAbility(bool isEnabled); 68 LocationErrCode RegisterSwitchCallback(const sptr<IRemoteObject>& callback, pid_t uid); 69 LocationErrCode UnregisterSwitchCallback(const sptr<IRemoteObject>& callback); 70 #ifdef FEATURE_GNSS_SUPPORT 71 LocationErrCode RegisterGnssStatusCallback(const sptr<IRemoteObject>& callback, pid_t uid); 72 LocationErrCode UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback); 73 LocationErrCode RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, pid_t uid); 74 LocationErrCode UnregisterNmeaMessageCallback(const sptr<IRemoteObject>& callback); 75 LocationErrCode RegisterCachedLocationCallback(std::unique_ptr<CachedGnssLocationsRequest>& request, 76 sptr<ICachedLocationsCallback>& callback, std::string bundleName); 77 LocationErrCode UnregisterCachedLocationCallback(sptr<ICachedLocationsCallback>& callback); 78 LocationErrCode GetCachedGnssLocationsSize(int& size); 79 LocationErrCode FlushCachedGnssLocations(); 80 LocationErrCode SendCommand(std::unique_ptr<LocationCommand>& commands); 81 LocationErrCode AddFence(std::unique_ptr<GeofenceRequest>& request); 82 LocationErrCode RemoveFence(std::unique_ptr<GeofenceRequest>& request); 83 #endif 84 LocationErrCode StartLocating(std::unique_ptr<RequestConfig>& requestConfig, 85 sptr<ILocatorCallback>& callback, AppIdentity &identity); 86 LocationErrCode StopLocating(sptr<ILocatorCallback>& callback); 87 LocationErrCode GetCacheLocation(std::unique_ptr<Location>& loc, AppIdentity &identity); 88 #ifdef FEATURE_GEOCODE_SUPPORT 89 LocationErrCode IsGeoConvertAvailable(bool &isAvailable); 90 void GetAddressByCoordinate(MessageParcel &data, MessageParcel &reply, std::string bundleName); 91 void GetAddressByLocationName(MessageParcel &data, MessageParcel &reply, std::string bundleName); 92 LocationErrCode EnableReverseGeocodingMock(); 93 LocationErrCode DisableReverseGeocodingMock(); 94 LocationErrCode SetReverseGeocodingMockInfo(std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo); 95 #endif 96 LocationErrCode IsLocationPrivacyConfirmed(const int type, bool& isConfirmed); 97 LocationErrCode SetLocationPrivacyConfirmStatus(const int type, bool isConfirmed); 98 LocationErrCode EnableLocationMock(); 99 LocationErrCode DisableLocationMock(); 100 LocationErrCode SetMockedLocations( 101 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location); 102 LocationErrCode ReportLocation(const std::unique_ptr<Location>& location, std::string abilityName); 103 LocationErrCode ReportLocationStatus(sptr<ILocatorCallback>& callback, int result); 104 LocationErrCode ReportErrorStatus(sptr<ILocatorCallback>& callback, int result); 105 LocationErrCode ProcessLocationMockMsg( 106 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId); 107 #ifdef FEATURE_GNSS_SUPPORT 108 LocationErrCode SendLocationMockMsgToGnssSa(const sptr<IRemoteObject> obj, 109 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId); 110 #endif 111 #ifdef FEATURE_NETWORK_SUPPORT 112 LocationErrCode SendLocationMockMsgToNetworkSa(const sptr<IRemoteObject> obj, 113 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId); 114 #endif 115 #ifdef FEATURE_PASSIVE_SUPPORT 116 LocationErrCode SendLocationMockMsgToPassiveSa(const sptr<IRemoteObject> obj, 117 const int timeInterval, const std::vector<std::shared_ptr<Location>> &location, int msgId); 118 #endif 119 LocationErrCode RegisterWifiScanInfoCallback(const sptr<IRemoteObject>& callback, pid_t uid); 120 LocationErrCode UnregisterWifiScanInfoCallback(const sptr<IRemoteObject>& callback); 121 LocationErrCode RegisterBluetoothScanInfoCallback(const sptr<IRemoteObject>& callback, pid_t uid); 122 LocationErrCode UnregisterBluetoothScanInfoCallback(const sptr<IRemoteObject>& callback); 123 LocationErrCode RegisterBleScanInfoCallback(const sptr<IRemoteObject>& callback, pid_t uid); 124 LocationErrCode UnregisterBleScanInfoCallback(const sptr<IRemoteObject>& callback); 125 126 std::shared_ptr<std::map<std::string, std::list<std::shared_ptr<Request>>>> GetRequests(); 127 std::shared_ptr<std::map<sptr<IRemoteObject>, std::list<std::shared_ptr<Request>>>> GetReceivers(); 128 std::shared_ptr<std::map<std::string, sptr<IRemoteObject>>> GetProxyMap(); 129 void UpdateSaAbilityHandler(); 130 void ApplyRequests(); 131 void RegisterAction(); 132 LocationErrCode ProxyUidForFreeze(int32_t uid, bool isProxy); 133 LocationErrCode ResetAllProxy(); 134 bool IsProxyUid(int32_t uid); 135 int GetActiveRequestNum(); 136 void RegisterPermissionCallback(const uint32_t callingTokenId, const std::vector<std::string>& permissionNameList); 137 void UnregisterPermissionCallback(const uint32_t callingTokenId); 138 void UnloadSaAbility(); 139 140 private: 141 bool Init(); 142 bool CheckSaValid(); 143 static int QuerySwitchState(); 144 #ifdef FEATURE_GEOCODE_SUPPORT 145 LocationErrCode SendGeoRequest(int type, MessageParcel &data, MessageParcel &reply); 146 #endif 147 #ifdef FEATURE_GNSS_SUPPORT 148 LocationErrCode SendGnssRequest(int type, MessageParcel &data, MessageParcel &reply); 149 #endif 150 void UpdateProxyMap(); 151 bool CheckIfLocatorConnecting(); 152 153 bool registerToAbility_ = false; 154 bool isActionRegistered = false; 155 std::string deviceId_; 156 ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START; 157 std::shared_ptr<LocatorEventSubscriber> locatorEventSubscriber_; 158 std::mutex switchMutex_; 159 std::mutex requestsMutex_; 160 std::mutex receiversMutex_; 161 std::mutex proxyMapMutex_; 162 std::mutex permissionMapMutex_; 163 std::unique_ptr<std::map<pid_t, sptr<ISwitchCallback>>> switchCallbacks_; 164 std::shared_ptr<std::map<std::string, std::list<std::shared_ptr<Request>>>> requests_; 165 std::shared_ptr<std::map<sptr<IRemoteObject>, std::list<std::shared_ptr<Request>>>> receivers_; 166 std::shared_ptr<std::map<std::string, sptr<IRemoteObject>>> proxyMap_; 167 std::shared_ptr<std::map<uint32_t, std::shared_ptr<PermissionStatusChangeCb>>> permissionMap_; 168 std::shared_ptr<LocatorHandler> locatorHandler_; 169 std::shared_ptr<RequestManager> requestManager_; 170 std::shared_ptr<ReportManager> reportManager_; 171 std::mutex proxyUidsMutex_; 172 std::set<int32_t> proxyUids_; 173 }; 174 } // namespace Location 175 } // namespace OHOS 176 #endif // LOCATOR_ABILITY_H 177