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 <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 
SetReportUserId(struct HiAppEvent_Processor * processor,const char * const * userIdNames,int size)135 int SetReportUserId(struct HiAppEvent_Processor* processor, const char* const * userIdNames, int size)
136 {
137     if (!IsApp()) {
138         return ErrorCode::ERROR_NOT_APP;
139     }
140     CHECK_PROCESSOR_PTR_AND_RETURN(processor, ErrorCode::ERROR_INVALID_PROCESSOR)
141     if (userIdNames == nullptr) {
142         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
143     }
144     for (int i = 0; i < size; ++i) {
145         if (userIdNames[i] == nullptr) {
146             return ErrorCode::ERROR_INVALID_PARAM_VALUE;
147         }
148         if (!IsValidUserIdName(userIdNames[i])) {
149             return ErrorCode::ERROR_INVALID_PARAM_VALUE_LENGTH;
150         }
151     }
152     auto ndkProcessorPtr = reinterpret_cast<NdkAppEventProcessor*>(processor);
153     return ndkProcessorPtr->SetReportUserId(userIdNames, size);
154 }
155 
SetReportUserProperty(struct HiAppEvent_Processor * processor,const char * const * userPropertyNames,int size)156 int SetReportUserProperty(struct HiAppEvent_Processor* processor, const char* const * userPropertyNames, int size)
157 {
158     if (!IsApp()) {
159         return ErrorCode::ERROR_NOT_APP;
160     }
161     CHECK_PROCESSOR_PTR_AND_RETURN(processor, ErrorCode::ERROR_INVALID_PROCESSOR)
162     if (userPropertyNames == nullptr) {
163         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
164     }
165     for (int i = 0; i < size; ++i) {
166         if (userPropertyNames[i] == nullptr) {
167             return ErrorCode::ERROR_INVALID_PARAM_VALUE;
168         }
169         if (!IsValidUserPropName(userPropertyNames[i])) {
170             return ErrorCode::ERROR_INVALID_PARAM_VALUE_LENGTH;
171         }
172     }
173     auto ndkProcessorPtr = reinterpret_cast<NdkAppEventProcessor*>(processor);
174     return ndkProcessorPtr->SetReportUserProperty(userPropertyNames, size);
175 }
176 
AddProcessor(struct HiAppEvent_Processor * processor)177 int64_t AddProcessor(struct HiAppEvent_Processor* processor)
178 {
179     if (!IsApp()) {
180         return ErrorCode::ERROR_NOT_APP;
181     }
182     CHECK_PROCESSOR_PTR_AND_RETURN(processor, ErrorCode::ERROR_INVALID_PROCESSOR)
183     auto ndkProcessorPtr = reinterpret_cast<NdkAppEventProcessor*>(processor);
184     HiAppEvent::ReportConfig config = ndkProcessorPtr->GetConfig();
185     if (VerifyReportConfig(config) != 0) {
186         HILOG_DEBUG(LOG_CORE, "faied to add processor=%{public}s, reportConfig is invalid", config.name.c_str());
187         return ErrorCode::ERROR_INVALID_PARAM_VALUE;
188     }
189     if (AppEventObserverMgr::GetInstance().Load(config.name) != 0) {
190         HILOG_DEBUG(LOG_CORE, "faied to add processor=%{public}s, name not found", config.name.c_str());
191         return ErrorCode::ERROR_UNKNOWN;
192     }
193     int64_t processorId = AppEventObserverMgr::GetInstance().RegisterObserver(config.name, config);
194     if (processorId <= 0) {
195         HILOG_DEBUG(LOG_CORE, "faied to add processor=%{public}s, register processor error", config.name.c_str());
196         return ErrorCode::ERROR_UNKNOWN;
197     }
198     return processorId;
199 }
200 
DestoryProcessor(struct HiAppEvent_Processor * processor)201 void DestoryProcessor(struct HiAppEvent_Processor* processor)
202 {
203     if (!IsApp()) {
204         return;
205     }
206     CHECK_PROCESSOR_PTR_AND_RETURN_VOID(processor)
207     auto ndkProcessorPtr = reinterpret_cast<NdkAppEventProcessor*>(processor);
208     delete ndkProcessorPtr;
209 }
210 
RemoveProcessor(int64_t processorId)211 int RemoveProcessor(int64_t processorId)
212 {
213     if (!IsApp()) {
214         return ErrorCode::ERROR_NOT_APP;
215     }
216     if (processorId <= 0) {
217         HILOG_DEBUG(LOG_CORE, "Failed to remove processor id=%{public}" PRId64, processorId);
218         return ErrorCode::ERROR_PROCESSOR_NOT_ADDED;
219     }
220     if (AppEventObserverMgr::GetInstance().UnregisterObserver(processorId) != 0) {
221         HILOG_DEBUG(LOG_CORE, "Failed to remove processor id=%{public}" PRId64, processorId);
222         return ErrorCode::ERROR_UNKNOWN;
223     }
224     return 0;
225 }
226