• 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 #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 HandlePermissionChanged(uint32_t tokenId);
67     void RegisterLocationErrorCallback(sptr<ILocatorCallback> callback, AppIdentity appIdentity);
68     void UnRegisterLocationErrorCallback(sptr<ILocatorCallback> callback);
69     void ReportLocationError(const int errorCode, std::shared_ptr<Request> request);
70     void SyncStillMovementState(bool state);
71     void SyncIdleState(bool state);
72     static RequestManager* GetInstance();
73     void IsStandby();
74     void UpdateLocationError(std::shared_ptr<Request> request);
75 
76 private:
77     bool RestorRequest(std::shared_ptr<Request> request);
78     void HandleChrEvent(std::list<std::shared_ptr<Request>> requests);
79     void UpdateRequestRecord(std::shared_ptr<Request> request, std::string abilityName, bool shouldInsert);
80     void DeleteRequestRecord(std::shared_ptr<std::list<std::shared_ptr<Request>>> requests);
81     void HandleRequest(std::string abilityName, std::list<std::shared_ptr<Request>> list);
82     void ProxySendLocationRequest(std::string abilityName, WorkRecord& workRecord);
83     sptr<IRemoteObject> GetRemoteObject(std::string abilityName);
84     bool IsUidInProcessing(int32_t uid);
85     bool UpdateUsingApproximatelyPermission(std::shared_ptr<Request> request, const bool isStart);
86     bool ActiveLocatingStrategies(const std::shared_ptr<Request>& request);
87     bool AddRequestToWorkRecord(std::string abilityName, std::shared_ptr<Request>& request,
88         std::shared_ptr<WorkRecord>& workRecord);
89     bool IsRequestAvailable(std::shared_ptr<Request>& request);
90     void UpdateRunningUids(const std::shared_ptr<Request>& request, std::string abilityName, bool isAdd);
91     void ReportDataToResSched(std::string state, const pid_t uid);
92     std::map<int32_t, int32_t> runningUidMap_;
93     std::map<sptr<IRemoteObject>, std::shared_ptr<LocationErrRequest>> locationErrorCallbackMap_;
94     static ffrt::mutex requestMutex_;
95     ffrt::mutex runningUidsMutex_;
96     ffrt::mutex locationErrorCallbackMutex_;
97     ffrt::mutex permissionRecordMutex_;
98     std::atomic_bool isDeviceIdleMode_;
99     std::atomic_bool isDeviceStillState_;
100 };
101 
102 class LocatorErrCallbackDeathRecipient : public IRemoteObject::DeathRecipient {
103 public:
104     void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
105     LocatorErrCallbackDeathRecipient(int32_t tokenId);
106     ~LocatorErrCallbackDeathRecipient() override;
107 private:
108     int32_t tokenId_;
109 };
110 } // namespace Location
111 } // namespace OHOS
112 #endif // REQUEST_MANAGER_H
113