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 #ifndef LOCATION_HOOK_UTILS_H
17 #define LOCATION_HOOK_UTILS_H
18 
19 #include "hookmgr.h"
20 #include "constant_definition.h"
21 #include "request.h"
22 #include "location.h"
23 
24 namespace OHOS {
25 namespace Location {
26 enum class LocationProcessStage {
27     LOCATOR_SA_START_LOCATING = 0,
28     LOCATOR_SA_REQUEST_PROCESS,
29     GNSS_SA_REQUEST_PROCESS,
30     NETWORK_SA_REQUEST_PROCESS,
31     GNSS_SA_LOCATION_REPORT_PROCESS,
32     NETWORK_SA_LOCATION_REPORT_PROCESS,
33     LOCATOR_SA_LOCATION_REPORT_PROCESS,
34     START_GNSS_PROCESS,
35     STOP_GNSS_PROCESS,
36     CHECK_GNSS_LOCATION_VALIDITY,
37     MOCK_LOCATION_PROCESS,
38     FENCE_REQUEST_PROCESS,
39     REQUEST_MANAGER_HANDLE_STOP,
40     LOCATOR_SA_GET_ADDRESSES_FROM_LOCATION_PROCESS,
41     LOCATOR_SA_GET_ADDRESSES_FROM_LOCATIONNAME_PROCESS,
42     WRITE_DFX_INNER_EVENT_PROCESS,
43     ADD_REQUEST_TO_WORK_RECORD,
44     LOCATOR_SA_LOCATION_PERMISSION_CHECK,
45     REPORT_MANAGER_GET_CACHE_LOCATION_PROCESS,
46     ENABLE_ABILITY_PROCESS,
47     PRE_START_LOCATING_PROCESS,
48     GNSS_REQUEST_RECORD_PROCESS,
49 };
50 
51 typedef struct {
52     Location location;
53     Request request;
54     std::string abilityName;
55     int retCode;
56 } LocationSupplicantInfo;
57 
58 typedef struct {
59     Location location;
60     bool result;
61 } GnssLocationValidStruct;
62 
63 typedef struct {
64     bool enableMock;
65 } MockLocationStruct;
66 
67 typedef struct {
68     int32_t eventId;
69     std::vector<std::string> names;
70     std::vector<std::string> values;
71 } DfxInnerInfo;
72 
73 typedef struct {
74     bool deviceStillState;
75     bool deviceIdleState;
76     bool result;
77     std::string abilityName;
78     std::string bundleName;
79 } LocatorRequestStruct;
80 
81 typedef struct {
82     bool isEnabled;
83     int32_t userId;
84     std::string bundleName;
85     bool result;
86 } EnableAbilityStruct;
87 
88 class HookUtils {
89 public:
90     static HOOK_MGR* GetLocationExtHookMgr();
91     static LocationErrCode RegisterHook(LocationProcessStage stage, int prio, OhosHook hook);
92     static void UnregisterHook(LocationProcessStage stage, OhosHook hook);
93     static LocationErrCode ExecuteHook(LocationProcessStage stage, void *executionContext,
94         const HOOK_EXEC_OPTIONS *options);
95     static void ExecuteHookWhenStartLocation(std::shared_ptr<Request> request);
96     static void ExecuteHookWhenStopLocation(std::shared_ptr<Request> request);
97     static void ExecuteHookWhenGetAddressFromLocation(std::string packageName);
98     static void ExecuteHookWhenGetAddressFromLocationName(std::string packageName);
99     static void ExecuteHookWhenReportInnerInfo(
100         int32_t event, std::vector<std::string>& names, std::vector<std::string>& values);
101     static bool ExecuteHookWhenAddWorkRecord(bool stillState, bool idleState, std::string abilityName,
102         std::string bundleName);
103     static bool CheckGnssLocationValidity(const std::unique_ptr<Location>& location);
104     static bool ExecuteHookWhenCheckAppForUser(std::string packageName);
105     static bool ExecuteHookWhenCheckAppForCacheTime(std::string packageName);
106     static bool ExecuteHookEnableAbility(std::string packageName, bool isEnabled, int32_t userId);
107     static bool ExecuteHookWhenPreStartLocating(std::string packageName);
108 };
109 } // namespace Location
110 } // namespace OHOS
111 #endif // LOCATION_HOOK_UTILS_H