• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 <cinttypes>
16 
17 #include "ndk_app_event_processor_service.h"
18 
19 #include "app_event_observer_mgr.h"
20 #include "base_type.h"
21 #include "hilog/log.h"
22 #include "hiappevent_base.h"
23 #include "hiappevent_verify.h"
24 #include "ndk_app_event_processor.h"
25 
26 #undef LOG_DOMAIN
27 #define LOG_DOMAIN 0xD002D07
28 
29 #undef LOG_TAG
30 #define LOG_TAG "NdkProcessorService"
31 
32 #ifndef CHECK_PROCESSOR_PTR_AND_RETURN_VOID
33 #define CHECK_PROCESSOR_PTR_AND_RETURN_VOID(ptr)  \
34     if ((ptr) == nullptr) {                       \
35         return;                                   \
36     }
37 #endif
38 
39 #ifndef CHECK_PROCESSOR_PTR_AND_RETURN
40 #define CHECK_PROCESSOR_PTR_AND_RETURN(ptr, ret)  \
41     if ((ptr) == nullptr) {                       \
42         return ret;                               \
43     }
44 #endif
45 
46 using namespace OHOS::HiviewDFX;
47 
CreateProcessor(const char * name)48 struct HiAppEvent_Processor* CreateProcessor(const char* name)
49 {
50     if (!IsApp()) {
51         HILOG_DEBUG(LOG_CORE, "caller is not app");
52         return nullptr;
53     }
54     CHECK_PROCESSOR_PTR_AND_RETURN(name, nullptr)
55     if (!IsValidProcessorName(name)) {
56         HILOG_DEBUG(LOG_CORE, "Invalid processor name");
57         return nullptr;
58     }
59     auto ndkProcessorPtr = new NdkAppEventProcessor(name);
60     return reinterpret_cast<HiAppEvent_Processor*>(ndkProcessorPtr);
61 }
62 
SetReportRoute(struct HiAppEvent_Processor * processor,const char * appId,const char * routeInfo)63 int SetReportRoute(struct HiAppEvent_Processor* processor, const char* appId, const char* routeInfo)
64 {
65     if (!IsApp()) {
66         return ErrorCode::ERROR_NOT_APP;
67     }
68     CHECK_PROCESSOR_PTR_AND_RETURN(processor, ErrorCode::ERROR_INVALID_PROCESSOR)
69     if (appId == nullptr || routeInfo == nullptr) {
70         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
71     }
72     if (!IsValidAppId(appId) || !IsValidRouteInfo(routeInfo)) {
73         return ErrorCode::ERROR_INVALID_PARAM_VALUE_LENGTH;
74     }
75     auto ndkProcessorPtr = reinterpret_cast<NdkAppEventProcessor*>(processor);
76     return ndkProcessorPtr->SetReportRoute(appId, routeInfo);
77 }
78 
SetReportPolicy(struct HiAppEvent_Processor * processor,int periodReport,int batchReport,bool onStartReport,bool onBackgroundReport)79 int SetReportPolicy(struct HiAppEvent_Processor* processor, int periodReport, int batchReport, bool onStartReport,
80     bool onBackgroundReport)
81 {
82     if (!IsApp()) {
83         return ErrorCode::ERROR_NOT_APP;
84     }
85     CHECK_PROCESSOR_PTR_AND_RETURN(processor, ErrorCode::ERROR_INVALID_PROCESSOR)
86     if (!IsValidPeriodReport(periodReport) || !IsValidBatchReport(batchReport)) {
87         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
88     }
89     auto ndkProcessorPtr = reinterpret_cast<NdkAppEventProcessor*>(processor);
90     return ndkProcessorPtr->SetReportPolicy(periodReport, batchReport, onStartReport, onBackgroundReport);
91 }
92 
SetReportEvent(struct HiAppEvent_Processor * processor,const char * domain,const char * name,bool isRealTime)93 int SetReportEvent(struct HiAppEvent_Processor* processor, const char* domain, const char* name, bool isRealTime)
94 {
95     if (!IsApp()) {
96         return ErrorCode::ERROR_NOT_APP;
97     }
98     CHECK_PROCESSOR_PTR_AND_RETURN(processor, ErrorCode::ERROR_INVALID_PROCESSOR)
99     if (domain == nullptr || name == nullptr || !IsValidDomain(domain) || !IsValidEventName(name)) {
100         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
101     }
102     auto ndkProcessorPtr = reinterpret_cast<NdkAppEventProcessor*>(processor);
103     return ndkProcessorPtr->SetReportEvent(domain, name, isRealTime);
104 }
105 
SetCustomConfig(struct HiAppEvent_Processor * processor,const char * key,const char * value)106 int SetCustomConfig(struct HiAppEvent_Processor* processor, const char* key, const char* value)
107 {
108     if (!IsApp()) {
109         return ErrorCode::ERROR_NOT_APP;
110     }
111     CHECK_PROCESSOR_PTR_AND_RETURN(processor, ErrorCode::ERROR_INVALID_PROCESSOR)
112     if (key == nullptr || value == nullptr) {
113         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
114     }
115     if (!IsValidCustomConfig(key, value)) {
116         return ErrorCode::ERROR_INVALID_PARAM_VALUE_LENGTH;
117     }
118     auto ndkProcessorPtr = reinterpret_cast<NdkAppEventProcessor*>(processor);
119     return ndkProcessorPtr->SetCustomConfig(key, value);
120 }
121 
SetConfigId(struct HiAppEvent_Processor * processor,int configId)122 int SetConfigId(struct HiAppEvent_Processor* processor, int configId)
123 {
124     if (!IsApp()) {
125         return ErrorCode::ERROR_NOT_APP;
126     }
127     CHECK_PROCESSOR_PTR_AND_RETURN(processor, ErrorCode::ERROR_INVALID_PROCESSOR)
128     if (!IsValidConfigId(configId)) {
129         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
130     }
131     auto ndkProcessorPtr = reinterpret_cast<NdkAppEventProcessor*>(processor);
132     return ndkProcessorPtr->SetConfigId(configId);
133 }
134 
SetConfigName(struct HiAppEvent_Processor * processor,const char * configName)135 int SetConfigName(struct HiAppEvent_Processor* processor, const char* configName)
136 {
137     if (!IsApp()) {
138         return ErrorCode::ERROR_NOT_APP;
139     }
140     CHECK_PROCESSOR_PTR_AND_RETURN(processor, ErrorCode::ERROR_INVALID_PROCESSOR)
141     if (!IsValidConfigNameLength(configName)) {
142         return ErrorCode::ERROR_INVALID_PARAM_VALUE_LENGTH;
143     }
144     if (!IsValidProcessorName(configName)) {
145         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
146     }
147     auto ndkProcessorPtr = reinterpret_cast<NdkAppEventProcessor*>(processor);
148     return ndkProcessorPtr->SetConfigName(configName) == ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL ?
149         ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL : ErrorCode::ERROR_INVALID_PARAM_VALUE;
150 }
151 
SetReportUserId(struct HiAppEvent_Processor * processor,const char * const * userIdNames,int size)152 int SetReportUserId(struct HiAppEvent_Processor* processor, const char* const * userIdNames, int size)
153 {
154     if (!IsApp()) {
155         return ErrorCode::ERROR_NOT_APP;
156     }
157     CHECK_PROCESSOR_PTR_AND_RETURN(processor, ErrorCode::ERROR_INVALID_PROCESSOR)
158     if (userIdNames == nullptr) {
159         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
160     }
161     for (int i = 0; i < size; ++i) {
162         if (userIdNames[i] == nullptr) {
163             return ErrorCode::ERROR_INVALID_PARAM_VALUE;
164         }
165         if (!IsValidUserIdName(userIdNames[i])) {
166             return ErrorCode::ERROR_INVALID_PARAM_VALUE_LENGTH;
167         }
168     }
169     auto ndkProcessorPtr = reinterpret_cast<NdkAppEventProcessor*>(processor);
170     return ndkProcessorPtr->SetReportUserId(userIdNames, size);
171 }
172 
SetReportUserProperty(struct HiAppEvent_Processor * processor,const char * const * userPropertyNames,int size)173 int SetReportUserProperty(struct HiAppEvent_Processor* processor, const char* const * userPropertyNames, int size)
174 {
175     if (!IsApp()) {
176         return ErrorCode::ERROR_NOT_APP;
177     }
178     CHECK_PROCESSOR_PTR_AND_RETURN(processor, ErrorCode::ERROR_INVALID_PROCESSOR)
179     if (userPropertyNames == nullptr) {
180         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
181     }
182     for (int i = 0; i < size; ++i) {
183         if (userPropertyNames[i] == nullptr) {
184             return ErrorCode::ERROR_INVALID_PARAM_VALUE;
185         }
186         if (!IsValidUserPropName(userPropertyNames[i])) {
187             return ErrorCode::ERROR_INVALID_PARAM_VALUE_LENGTH;
188         }
189     }
190     auto ndkProcessorPtr = reinterpret_cast<NdkAppEventProcessor*>(processor);
191     return ndkProcessorPtr->SetReportUserProperty(userPropertyNames, size);
192 }
193 
AddProcessor(struct HiAppEvent_Processor * processor)194 int64_t AddProcessor(struct HiAppEvent_Processor* processor)
195 {
196     if (!IsApp()) {
197         return ErrorCode::ERROR_NOT_APP;
198     }
199     CHECK_PROCESSOR_PTR_AND_RETURN(processor, ErrorCode::ERROR_INVALID_PROCESSOR)
200     auto ndkProcessorPtr = reinterpret_cast<NdkAppEventProcessor*>(processor);
201     HiAppEvent::ReportConfig config = ndkProcessorPtr->GetConfig();
202     if (VerifyReportConfig(config) != 0) {
203         HILOG_DEBUG(LOG_CORE, "faied to add processor=%{public}s, reportConfig is invalid", config.name.c_str());
204         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
205     }
206     if (AppEventObserverMgr::GetInstance().Load(config.name) != 0) {
207         HILOG_DEBUG(LOG_CORE, "faied to add processor=%{public}s, name not found", config.name.c_str());
208         return ErrorCode::ERROR_UNKNOWN;
209     }
210     int64_t processorId = AppEventObserverMgr::GetInstance().AddProcessor(config.name, config);
211     if (processorId <= 0) {
212         HILOG_DEBUG(LOG_CORE, "faied to add processor=%{public}s, register processor error", config.name.c_str());
213         return ErrorCode::ERROR_UNKNOWN;
214     }
215     return processorId;
216 }
217 
DestroyProcessor(struct HiAppEvent_Processor * processor)218 void DestroyProcessor(struct HiAppEvent_Processor* processor)
219 {
220     if (!IsApp()) {
221         return;
222     }
223     CHECK_PROCESSOR_PTR_AND_RETURN_VOID(processor)
224     auto ndkProcessorPtr = reinterpret_cast<NdkAppEventProcessor*>(processor);
225     delete ndkProcessorPtr;
226 }
227 
RemoveProcessor(int64_t processorId)228 int RemoveProcessor(int64_t processorId)
229 {
230     if (!IsApp()) {
231         return ErrorCode::ERROR_NOT_APP;
232     }
233     if (processorId <= 0) {
234         HILOG_DEBUG(LOG_CORE, "Failed to remove processor id=%{public}" PRId64, processorId);
235         return ErrorCode::ERROR_PROCESSOR_NOT_ADDED;
236     }
237     if (AppEventObserverMgr::GetInstance().RemoveObserver(processorId) != 0) {
238         HILOG_DEBUG(LOG_CORE, "Failed to remove processor id=%{public}" PRId64, processorId);
239         return ErrorCode::ERROR_UNKNOWN;
240     }
241     return 0;
242 }
243