• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef ACCESSIBILITY_API_EVENT_REPORTER_H
17 #define ACCESSIBILITY_API_EVENT_REPORTER_H
18 #include <string>
19 #include "singleton.h"
20 
21 namespace OHOS {
22 namespace Accessibility {
23 #define ACCESSIBILITY_API_OPERATION_CONFIG_PATH "/etc/accessibility/api_event_reporter.cfg"
24 struct ApiReportConfig {
25     std::string config_name;
26     std::string config_appId;
27     std::string config_routeInfo;
28     int config_timeout = 0;
29     int config_row = 0;
30 };
31 
32 struct ApiEvent {
33     std::string domain;
34     std::string name;
35     bool isRealTime = false;
36 };
37 
38 struct ApiEventConfig {
39     ApiEvent event1;
40     ApiEvent event2;
41     ApiEvent event3;
42 };
43 
44 struct EventPeriodExpandableData {
45     std::vector<int64_t> runTime;
46     int64_t sumTime;
47     int64_t successCount;
48 };
49 
50 class ApiEventReporter : public Singleton<ApiEventReporter> {
51     DECLARE_SINGLETON(ApiEventReporter);
52 public:
53     int64_t AddProcessor();
54     void WriteEndEvent(int result, int errCode, std::string apiName, int64_t beginTime);
55     void ThresholdWriteEndEvent(int result, std::string apiName, int64_t beginTime,
56         int32_t thresholdValue);
57     int64_t GetCurrentTime();
58     void ClearCacheData();
59 
60 private:
61     static bool LoadConfigurationFile(const std::string &configFile);
62     static void GetConfigurationParams(ApiReportConfig &reportConfig, ApiEventConfig &eventConfig);
63     static std::string Trim(const std::string &str);
64     static std::pair<std::string, std::string> ParseKeyValue(const std::string &line);
65     static bool TryParseInt(const std::string &str, int &out);
66     static void ParseReportConfig(std::istringstream &stream, ApiReportConfig &reportConfig);
67     static void ParseEvent(std::istringstream &stream, ApiEvent &event);
68     static void ParseEventConfig(std::istringstream &stream, ApiEventConfig &eventConfig);
69     static void ParseApiOperationManagement(std::istringstream &stream, ApiReportConfig &reportConfig,
70         ApiEventConfig &eventConfig);
71     std::shared_ptr<EventPeriodExpandableData> CacheEventInfo(std::string apiName,
72         int64_t beginTime, int result);
73     bool IsAppEventProccessorValid();
74     void ExecuteThresholdWriteEndEvent(std::string apiName,
75         std::shared_ptr<EventPeriodExpandableData> expandableData, int32_t dataCount);
76     static std::string g_fileContent;
77     static std::mutex g_apiOperationMutex;
78     static int64_t g_processorId;
79     static const int64_t NULLPTR_PROCCESSORID = 0;
80     std::unordered_map<std::string, std::shared_ptr<EventPeriodExpandableData>> m_thresholdData;
81     static bool IsValidPath(const std::string& filePath);
82     static bool IsReal(const std::string& file, std::string& realFile);
83 };
84 }  // namespace Accessibility
85 }  // namespace OHOS
86 #endif  // ACCESSIBILITY_API_EVENT_REPORTER_H