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 #include "hook_utils.h"
16 #include "location_log.h"
17 #include "constant_definition.h"
18 
19 namespace OHOS {
20 namespace Location {
21 static HOOK_MGR* locatorHookMgr_ = nullptr;
22 
GetLocationExtHookMgr()23 HOOK_MGR* HookUtils::GetLocationExtHookMgr()
24 {
25     if (locatorHookMgr_ != nullptr) {
26         return locatorHookMgr_;
27     }
28 
29     locatorHookMgr_ = HookMgrCreate("locationHookMgr");
30     return locatorHookMgr_;
31 }
32 
RegisterHook(LocationProcessStage stage,int prio,OhosHook hook)33 LocationErrCode HookUtils::RegisterHook(LocationProcessStage stage, int prio, OhosHook hook)
34 {
35     auto ret = HookMgrAdd(GetLocationExtHookMgr(), static_cast<int>(stage), prio, hook);
36     if (ret == 0) {
37         return ERRCODE_SUCCESS;
38     }
39     LBSLOGE(LOCATOR, "%{public}s stage = %{public}d add failed ret = %{public}d",
40         __func__, static_cast<int>(stage), ret);
41     return ERRCODE_SERVICE_UNAVAILABLE;
42 }
43 
UnregisterHook(LocationProcessStage stage,OhosHook hook)44 void HookUtils::UnregisterHook(LocationProcessStage stage, OhosHook hook)
45 {
46     HookMgrDel(GetLocationExtHookMgr(), static_cast<int>(stage), hook);
47 }
48 
ExecuteHook(LocationProcessStage stage,void * executionContext,const HOOK_EXEC_OPTIONS * options)49 LocationErrCode HookUtils::ExecuteHook(
50     LocationProcessStage stage, void *executionContext, const HOOK_EXEC_OPTIONS *options)
51 {
52     auto ret = HookMgrExecute(GetLocationExtHookMgr(), static_cast<int>(stage), executionContext, options);
53     if (ret == 0) {
54         return ERRCODE_SUCCESS;
55     }
56     LBSLOGE(LOCATOR, "%{public}s stage = %{public}d execute failed ret = %{public}d",
57         __func__, static_cast<int>(stage), ret);
58     return ERRCODE_SERVICE_UNAVAILABLE;
59 }
60 
ExecuteHookWhenStartLocation(std::shared_ptr<Request> request)61 void HookUtils::ExecuteHookWhenStartLocation(std::shared_ptr<Request> request)
62 {
63     LocationSupplicantInfo reportStruct;
64     reportStruct.request = *request;
65     reportStruct.retCode = true;
66     ExecuteHook(LocationProcessStage::LOCATOR_SA_START_LOCATING, (void *)&reportStruct, nullptr);
67 }
68 
ExecuteHookWhenStopLocation(std::shared_ptr<Request> request)69 void HookUtils::ExecuteHookWhenStopLocation(std::shared_ptr<Request> request)
70 {
71     LocationSupplicantInfo reportStruct;
72     reportStruct.request = *request;
73     reportStruct.retCode = true;
74     ExecuteHook(LocationProcessStage::REQUEST_MANAGER_HANDLE_STOP, (void *)&reportStruct, nullptr);
75 }
76 
ExecuteHookWhenGetAddressFromLocation(std::string packageName)77 void HookUtils::ExecuteHookWhenGetAddressFromLocation(std::string packageName)
78 {
79     LocationSupplicantInfo reportStruct;
80     reportStruct.abilityName = packageName;
81     reportStruct.retCode = true;
82     ExecuteHook(
83         LocationProcessStage::LOCATOR_SA_GET_ADDRESSES_FROM_LOCATION_PROCESS, (void *)&reportStruct, nullptr);
84 }
85 
ExecuteHookWhenGetAddressFromLocationName(std::string packageName)86 void HookUtils::ExecuteHookWhenGetAddressFromLocationName(std::string packageName)
87 {
88     LocationSupplicantInfo reportStruct;
89     reportStruct.abilityName = packageName;
90     reportStruct.retCode = true;
91     ExecuteHook(
92         LocationProcessStage::LOCATOR_SA_GET_ADDRESSES_FROM_LOCATIONNAME_PROCESS, (void *)&reportStruct, nullptr);
93 }
94 
ExecuteHookWhenReportInnerInfo(int32_t event,std::vector<std::string> & names,std::vector<std::string> & values)95 void HookUtils::ExecuteHookWhenReportInnerInfo(
96     int32_t event, std::vector<std::string>& names, std::vector<std::string>& values)
97 {
98     DfxInnerInfo innerInfo;
99     innerInfo.eventId = event;
100     innerInfo.names = names;
101     innerInfo.values = values;
102     ExecuteHook(LocationProcessStage::WRITE_DFX_INNER_EVENT_PROCESS, (void *)&innerInfo, nullptr);
103 }
104 
ExecuteHookWhenAddWorkRecord(bool stillState,bool idleState,std::string abilityName,std::string bundleName)105 bool HookUtils::ExecuteHookWhenAddWorkRecord(bool stillState, bool idleState, std::string abilityName,
106     std::string bundleName)
107 {
108     LocatorRequestStruct locatorRequestStruct;
109     locatorRequestStruct.deviceStillState = stillState;
110     locatorRequestStruct.deviceIdleState = idleState;
111     locatorRequestStruct.abilityName = abilityName;
112     locatorRequestStruct.bundleName = bundleName;
113     locatorRequestStruct.result = false;
114     ExecuteHook(
115         LocationProcessStage::ADD_REQUEST_TO_WORK_RECORD, (void *)&locatorRequestStruct, nullptr);
116     return locatorRequestStruct.result;
117 }
118 
ExecuteHookWhenCheckAppForUser(std::string packageName)119 bool HookUtils::ExecuteHookWhenCheckAppForUser(std::string packageName)
120 {
121     LocatorRequestStruct locatorRequestStruct;
122     locatorRequestStruct.bundleName = packageName;
123     locatorRequestStruct.result = false;
124     ExecuteHook(
125         LocationProcessStage::LOCATOR_SA_LOCATION_PERMISSION_CHECK, (void *)&locatorRequestStruct, nullptr);
126     return locatorRequestStruct.result;
127 }
128 
CheckGnssLocationValidity(const std::unique_ptr<Location> & location)129 bool HookUtils::CheckGnssLocationValidity(const std::unique_ptr<Location>& location)
130 {
131     GnssLocationValidStruct gnssLocationValidStruct;
132     gnssLocationValidStruct.location = *location;
133     gnssLocationValidStruct.result = true;
134     HookUtils::ExecuteHook(
135         LocationProcessStage::CHECK_GNSS_LOCATION_VALIDITY, (void *)&gnssLocationValidStruct, nullptr);
136     return gnssLocationValidStruct.result;
137 }
138 
ExecuteHookWhenCheckAppForCacheTime(std::string packageName)139 bool HookUtils::ExecuteHookWhenCheckAppForCacheTime(std::string packageName)
140 {
141     LocatorRequestStruct locatorRequestStruct;
142     locatorRequestStruct.bundleName = packageName;
143     locatorRequestStruct.result = false;
144     ExecuteHook(
145         LocationProcessStage::REPORT_MANAGER_GET_CACHE_LOCATION_PROCESS, (void *)&locatorRequestStruct, nullptr);
146     return locatorRequestStruct.result;
147 }
148 
ExecuteHookEnableAbility(std::string packageName,bool isEnabled,int32_t userId)149 bool HookUtils::ExecuteHookEnableAbility(std::string packageName, bool isEnabled, int32_t userId)
150 {
151     EnableAbilityStruct enableAbilityStruct;
152     enableAbilityStruct.bundleName = packageName;
153     enableAbilityStruct.isEnabled = isEnabled;
154     enableAbilityStruct.userId = userId;
155     enableAbilityStruct.result = true;
156     ExecuteHook(
157         LocationProcessStage::ENABLE_ABILITY_PROCESS, (void *)&enableAbilityStruct, nullptr);
158     return enableAbilityStruct.result;
159 }
160 
ExecuteHookWhenPreStartLocating(std::string packageName)161 bool HookUtils::ExecuteHookWhenPreStartLocating(std::string packageName)
162 {
163     LocatorRequestStruct locatorRequestStruct;
164     locatorRequestStruct.bundleName = packageName;
165     locatorRequestStruct.result = true;
166     ExecuteHook(
167         LocationProcessStage::PRE_START_LOCATING_PROCESS, (void *)&locatorRequestStruct, nullptr);
168     return locatorRequestStruct.result;
169 }
170 } // namespace Location
171 } // namespace OHOS
172