• 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 
16 #include <cinttypes>
17 
18 #include "ndk_app_event_processor.h"
19 
20 #include "app_event_observer_mgr.h"
21 #include "hiappevent_verify.h"
22 #include "hilog/log.h"
23 #include "processor_config_loader.h"
24 
25 #undef LOG_DOMAIN
26 #define LOG_DOMAIN 0xD002D07
27 
28 #undef LOG_TAG
29 #define LOG_TAG "NdkProcessor"
30 
31 namespace OHOS {
32 namespace HiviewDFX {
33 namespace {
34 constexpr int CODE_SUCC = 0;
35 constexpr int CODE_FAILED = -1;
36 }
37 
NdkAppEventProcessor(const std::string & name)38 NdkAppEventProcessor::NdkAppEventProcessor(const std::string &name)
39 {
40     config_.name = name;
41 }
42 
SetReportRoute(const char * appId,const char * routeInfo)43 int NdkAppEventProcessor::SetReportRoute(const char* appId, const char* routeInfo)
44 {
45     config_.appId = appId;
46     config_.routeInfo = routeInfo;
47     return CODE_SUCC;
48 }
49 
SetReportPolicy(int periodReport,int batchReport,bool onStartReport,bool onBackgroundReport)50 int NdkAppEventProcessor::SetReportPolicy(int periodReport, int batchReport, bool onStartReport,
51     bool onBackgroundReport)
52 {
53     config_.triggerCond.timeout = periodReport;
54     config_.triggerCond.row = batchReport;
55     config_.triggerCond.onStartup = onStartReport;
56     config_.triggerCond.onBackground = onBackgroundReport;
57     return CODE_SUCC;
58 }
59 
SetReportEvent(const char * domain,const char * name,bool isRealTime)60 int NdkAppEventProcessor::SetReportEvent(const char* domain, const char* name, bool isRealTime)
61 {
62     EventConfig event;
63     event.domain = domain;
64     event.name = name;
65     event.isRealTime = isRealTime;
66     config_.eventConfigs.emplace_back(event);
67     return CODE_SUCC;
68 }
69 
SetCustomConfig(const char * key,const char * value)70 int NdkAppEventProcessor::SetCustomConfig(const char* key, const char* value)
71 {
72     auto it = config_.customConfigs.find(key);
73     if (it != config_.customConfigs.end()) {
74         config_.customConfigs.erase(it);
75     }
76     config_.customConfigs.insert(std::pair<std::string, std::string>(key, value));
77     return CODE_SUCC;
78 }
79 
SetConfigId(int configId)80 int NdkAppEventProcessor::SetConfigId(int configId)
81 {
82     config_.configId = configId;
83     return CODE_SUCC;
84 }
85 
SetConfigName(const std::string & configName)86 int NdkAppEventProcessor::SetConfigName(const std::string& configName)
87 {
88     config_.configName = configName;
89     HiAppEvent::ProcessorConfigLoader loader;
90     if (loader.LoadProcessorConfig(config_.name, config_.configName)) {
91         config_ = loader.GetReportConfig();
92     } else {
93         HILOG_WARN(LOG_CORE, "failed to load config content, configName:%{public}s", configName.c_str());
94         return CODE_FAILED;
95     }
96     return CODE_SUCC;
97 }
98 
SetReportUserId(const char * const * userIdNames,int size)99 int NdkAppEventProcessor::SetReportUserId(const char* const * userIdNames, int size)
100 {
101     for (int i = 0; i < size; ++i) {
102         config_.userIdNames.emplace(userIdNames[i]);
103     }
104     return CODE_SUCC;
105 }
106 
SetReportUserProperty(const char * const * userPropertyNames,int size)107 int NdkAppEventProcessor::SetReportUserProperty(const char* const * userPropertyNames, int size)
108 {
109     for (int i = 0; i < size; ++i) {
110         config_.userPropertyNames.emplace(userPropertyNames[i]);
111     }
112     return CODE_SUCC;
113 }
114 
GetConfig()115 HiAppEvent::ReportConfig NdkAppEventProcessor::GetConfig()
116 {
117     return config_;
118 }
119 }
120 }