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 REQUEST_MANAGER_H 17 #define REQUEST_MANAGER_H 18 19 #include <list> 20 #include <map> 21 #include <mutex> 22 #include <singleton.h> 23 #include <string> 24 25 #include "ffrt.h" 26 #include "iremote_object.h" 27 28 #include "i_locator_callback.h" 29 #include "request.h" 30 #include "work_record.h" 31 32 namespace OHOS { 33 namespace Location { 34 class LocationErrRequest { 35 public: 36 LocationErrRequest(); 37 ~LocationErrRequest(); 38 pid_t GetUid(); 39 void SetUid(pid_t uid); 40 pid_t GetPid(); 41 void SetPid(pid_t pid); 42 int32_t GetLastReportErrcode(); 43 void SetLastReportErrcode(int32_t lastReportErrcode); 44 void SetLocatorErrCallbackRecipient(const sptr<IRemoteObject::DeathRecipient>& recipient); 45 sptr<IRemoteObject::DeathRecipient> GetLocatorErrCallbackRecipient(); 46 private: 47 void GetProxyNameByPriority(std::shared_ptr<std::list<std::string>> proxys); 48 49 pid_t uid_; 50 pid_t pid_; 51 int32_t lastReportErrcode_; 52 sptr<IRemoteObject::DeathRecipient> locatorErrCallbackRecipient_; 53 }; 54 55 class RequestManager { 56 public: 57 RequestManager(); 58 ~RequestManager(); 59 bool InitSystemListeners(); 60 void HandleStartLocating(std::shared_ptr<Request> request); 61 void HandleStopLocating(sptr<ILocatorCallback> callback); 62 void HandlePowerSuspendChanged(int32_t pid, int32_t uid, int32_t flag); 63 void UpdateRequestRecord(std::shared_ptr<Request> request, bool shouldInsert); 64 void HandleRequest(); 65 bool UpdateUsingPermission(std::shared_ptr<Request> request, const bool isStart); 66 void IncreaseWorkingPidsCount(const pid_t pid); 67 void DecreaseWorkingPidsCount(const pid_t pid); 68 bool IsNeedStartUsingPermission(const pid_t pid); 69 bool IsNeedStopUsingPermission(const pid_t pid); 70 void RegisterLocationErrorCallback(sptr<ILocatorCallback> callback, AppIdentity appIdentity); 71 void UnRegisterLocationErrorCallback(sptr<ILocatorCallback> callback); 72 void ReportLocationError(const int errorCode, std::shared_ptr<Request> request); 73 void SyncStillMovementState(bool state); 74 void SyncIdleState(bool state); 75 static RequestManager* GetInstance(); 76 void IsStandby(); 77 void UpdateLocationError(std::shared_ptr<Request> request); 78 79 private: 80 bool RestorRequest(std::shared_ptr<Request> request); 81 void HandleChrEvent(std::list<std::shared_ptr<Request>> requests); 82 void UpdateRequestRecord(std::shared_ptr<Request> request, std::string abilityName, bool shouldInsert); 83 void DeleteRequestRecord(std::shared_ptr<std::list<std::shared_ptr<Request>>> requests); 84 void HandleRequest(std::string abilityName, std::list<std::shared_ptr<Request>> list); 85 void ProxySendLocationRequest(std::string abilityName, WorkRecord& workRecord); 86 sptr<IRemoteObject> GetRemoteObject(std::string abilityName); 87 bool IsUidInProcessing(int32_t uid); 88 bool UpdateUsingApproximatelyPermission(std::shared_ptr<Request> request, const bool isStart); 89 bool ActiveLocatingStrategies(const std::shared_ptr<Request>& request); 90 bool AddRequestToWorkRecord(std::string abilityName, std::shared_ptr<Request>& request, 91 std::shared_ptr<WorkRecord>& workRecord); 92 bool IsRequestAvailable(std::shared_ptr<Request>& request); 93 void UpdateRunningUids(const std::shared_ptr<Request>& request, std::string abilityName, bool isAdd); 94 void ReportDataToResSched(std::string state, const pid_t pid, const pid_t uid); 95 std::map<int32_t, int32_t> runningUidMap_; 96 std::map<sptr<IRemoteObject>, std::shared_ptr<LocationErrRequest>> locationErrorCallbackMap_; 97 static ffrt::mutex requestMutex_; 98 ffrt::mutex runningUidsMutex_; 99 ffrt::mutex locationErrorCallbackMutex_; 100 ffrt::mutex permissionRecordMutex_; 101 std::atomic_bool isDeviceIdleMode_; 102 std::atomic_bool isDeviceStillState_; 103 ffrt::mutex workingPidsCountMutex_; 104 std::map<pid_t, int32_t> workingPidsCountMap_; 105 }; 106 107 class LocatorErrCallbackDeathRecipient : public IRemoteObject::DeathRecipient { 108 public: 109 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 110 LocatorErrCallbackDeathRecipient(int32_t tokenId); 111 ~LocatorErrCallbackDeathRecipient() override; 112 private: 113 int32_t tokenId_; 114 }; 115 } // namespace Location 116 } // namespace OHOS 117 #endif // REQUEST_MANAGER_H 118