• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2  * Copyright (C) 2024 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 #include "self_request_manager.h"
17 
18 #include "common_utils.h"
19 #include "constant_definition.h"
20 #include "location_config_manager.h"
21 #include "location_log.h"
22 #include "locator_ability.h"
23 #include "request_manager.h"
24 #include "permission_manager.h"
25 #include "location_data_rdb_manager.h"
26 #include "ipc_skeleton.h"
27 #ifdef LOCATION_HICOLLIE_ENABLE
28 #include "xcollie/xcollie.h"
29 #include "xcollie/xcollie_define.h"
30 #endif
31 
32 namespace OHOS {
33 namespace Location {
34 std::mutex SelfRequestManager::locatorMutex_;
35 const uint32_t EVENT_STARTLOCATING = 0x0100;
36 const uint32_t EVENT_STOPLOCATING = 0x0200;
37 const int TIMEOUT_WATCHDOG = 60; // s
GetInstance()38 SelfRequestManager* SelfRequestManager::GetInstance()
39 {
40     static SelfRequestManager data;
41     return &data;
42 }
43 
SelfRequestManager()44 SelfRequestManager::SelfRequestManager()
45 {
46     selfRequestManagerHandler_ = std::make_shared<SelfRequestManagerHandler>(AppExecFwk::EventRunner::Create(true,
47         AppExecFwk::ThreadMode::FFRT));
48     callback_ = sptr<mLocatorCallback>(new (std::nothrow) SelfRequestManager::mLocatorCallback());
49     if (callback_ == nullptr) {
50         return;
51     }
52     request_ = std::make_shared<Request>();
53 }
54 
~SelfRequestManager()55 SelfRequestManager::~SelfRequestManager()
56 {
57 }
58 
ProcessStartSelfRequestEvent(const std::shared_ptr<Request> & request)59 void SelfRequestManager::ProcessStartSelfRequestEvent(const std::shared_ptr<Request>& request)
60 {
61     if (isLocating_ || !(LocationDataRdbManager::QuerySwitchState() == ENABLED)
62         || request_ == nullptr || request == nullptr || request->GetRequestConfig() == nullptr) {
63         LBSLOGD(LOCATOR, "cancel locating");
64         return;
65     }
66     isLocating_ = true;
67     request_->SetUid(request->GetUid());
68     request_->SetPid(request->GetPid());
69     request_->SetPackageName(request->GetPackageName());
70     request_->SetRequestConfig(*request->GetRequestConfig());
71     request_->SetUuid(request->GetUuid());
72     request_->SetTokenId(IPCSkeleton::GetCallingTokenID());
73     request_->SetTokenIdEx(IPCSkeleton::GetCallingFullTokenID());
74     request_->SetLocatorCallBack(callback_);
75     LBSLOGI(LOCATOR, "SelfRequestManager start locating");
76     LocatorAbility::GetInstance()->HandleStartLocating(request_, callback_);
77 }
78 
ProcessStopSelfRequestEvent()79 void SelfRequestManager::ProcessStopSelfRequestEvent()
80 {
81     if (!isLocating_) {
82         return;
83     }
84     isLocating_ = false;
85     LocatorAbility::GetInstance()->StopLocating(callback_);
86     LBSLOGI(LOCATOR, "SelfRequestManager stop locating");
87 }
88 
StopSelfRequest()89 void SelfRequestManager::StopSelfRequest()
90 {
91     if (selfRequestManagerHandler_ != nullptr) {
92         selfRequestManagerHandler_->SendHighPriorityEvent(EVENT_STOPLOCATING, 0, 0);
93     }
94 }
95 
StartSelfRequest(const std::shared_ptr<Request> & request)96 void SelfRequestManager::StartSelfRequest(const std::shared_ptr<Request>& request)
97 {
98     if (selfRequestManagerHandler_ != nullptr) {
99         selfRequestManagerHandler_->SendHighPriorityEvent(EVENT_STARTLOCATING, request, 0);
100         selfRequestManagerHandler_->SendHighPriorityEvent(EVENT_STOPLOCATING, 0, DEFAULT_TIMEOUT_5S);
101     }
102 }
103 
OnLocationReport(const std::unique_ptr<Location> & location)104 void SelfRequestManager::mLocatorCallback::OnLocationReport(const std::unique_ptr<Location>& location)
105 {
106     LBSLOGD(LOCATOR, "locator background OnLocationReport");
107     SelfRequestManager::GetInstance()->StopSelfRequest();
108 }
109 
OnLocatingStatusChange(const int status)110 void SelfRequestManager::mLocatorCallback::OnLocatingStatusChange(const int status)
111 {
112 }
113 
OnErrorReport(const int errorCode)114 void SelfRequestManager::mLocatorCallback::OnErrorReport(const int errorCode)
115 {
116 }
117 
SelfRequestManagerHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner)118 SelfRequestManagerHandler::SelfRequestManagerHandler(
119     const std::shared_ptr<AppExecFwk::EventRunner>& runner) : EventHandler(runner)
120 {
121     InitSelfRequestManagerHandlerEventMap();
122 }
123 
~SelfRequestManagerHandler()124 SelfRequestManagerHandler::~SelfRequestManagerHandler() {}
125 
InitSelfRequestManagerHandlerEventMap()126 void SelfRequestManagerHandler::InitSelfRequestManagerHandlerEventMap()
127 {
128     if (selfRequestManagerHandlerEventMap_.size() != 0) {
129         return;
130     }
131     selfRequestManagerHandlerEventMap_[EVENT_STARTLOCATING] =
132         [this](const AppExecFwk::InnerEvent::Pointer& event) { StartLocatingEvent(event); };
133     selfRequestManagerHandlerEventMap_[EVENT_STOPLOCATING] =
134         [this](const AppExecFwk::InnerEvent::Pointer& event) { StopLocatingEvent(event); };
135 }
136 
StartLocatingEvent(const AppExecFwk::InnerEvent::Pointer & event)137 void SelfRequestManagerHandler::StartLocatingEvent(const AppExecFwk::InnerEvent::Pointer& event)
138 {
139     auto SelfRequestManager = SelfRequestManager::GetInstance();
140     std::shared_ptr<Request> request = event->GetSharedObject<Request>();
141     SelfRequestManager->ProcessStartSelfRequestEvent(request);
142 }
143 
StopLocatingEvent(const AppExecFwk::InnerEvent::Pointer & event)144 void SelfRequestManagerHandler::StopLocatingEvent(const AppExecFwk::InnerEvent::Pointer& event)
145 {
146     auto SelfRequestManager = SelfRequestManager::GetInstance();
147     SelfRequestManager->ProcessStopSelfRequestEvent();
148 }
149 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)150 void SelfRequestManagerHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
151 {
152     uint32_t eventId = event->GetInnerEventId();
153     LBSLOGD(LOCATOR, "SelfRequestManagerHandler processEvent event:%{public}d, timestamp = %{public}s",
154         eventId, std::to_string(CommonUtils::GetCurrentTimeStamp()).c_str());
155     auto handleFunc = selfRequestManagerHandlerEventMap_.find(eventId);
156     if (handleFunc != selfRequestManagerHandlerEventMap_.end() && handleFunc->second != nullptr) {
157         auto memberFunc = handleFunc->second;
158 #ifdef LOCATION_HICOLLIE_ENABLE
159         int tid = gettid();
160         std::string moduleName = "SelfRequestManagerHandler";
161         XCollieCallback callbackFunc = [moduleName, eventId, tid](void *) {
162             LBSLOGE(LOCATOR, "TimeoutCallback tid:%{public}d moduleName:%{public}s excute eventId:%{public}u timeout.",
163                 tid, moduleName.c_str(), eventId);
164         };
165         std::string dfxInfo = moduleName + "_" + std::to_string(eventId) + "_" + std::to_string(tid);
166         int timerId = HiviewDFX::XCollie::GetInstance().SetTimer(dfxInfo, TIMEOUT_WATCHDOG, callbackFunc, nullptr,
167             HiviewDFX::XCOLLIE_FLAG_LOG|HiviewDFX::XCOLLIE_FLAG_RECOVERY);
168         memberFunc(event);
169         HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
170 #else
171         memberFunc(event);
172 #endif
173     }
174 }
175 } // namespace OHOS
176 } // namespace Location
177