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 ThresholdWriteEndEvent(int result, std::string apiName, int64_t beginTime, 55 int32_t thresholdValue); 56 int64_t GetCurrentTime(); 57 void ClearCacheData(); 58 59 private: 60 static bool LoadConfigurationFile(const std::string &configFile); 61 static void GetConfigurationParams(ApiReportConfig &reportConfig, ApiEventConfig &eventConfig); 62 static std::string Trim(const std::string &str); 63 static std::pair<std::string, std::string> ParseKeyValue(const std::string &line); 64 static bool TryParseInt(const std::string &str, int &out); 65 static void ParseReportConfig(std::istringstream &stream, ApiReportConfig &reportConfig); 66 static void ParseEvent(std::istringstream &stream, ApiEvent &event); 67 static void ParseEventConfig(std::istringstream &stream, ApiEventConfig &eventConfig); 68 static void ParseApiOperationManagement(std::istringstream &stream, ApiReportConfig &reportConfig, 69 ApiEventConfig &eventConfig); 70 std::shared_ptr<EventPeriodExpandableData> CacheEventInfo(std::string apiName, 71 int64_t beginTime, int result); 72 bool IsAppEventProccessorValid(); 73 void ExecuteThresholdWriteEndEvent(std::string apiName, 74 std::shared_ptr<EventPeriodExpandableData> expandableData, int32_t dataCount); 75 static std::string g_fileContent; 76 static std::mutex g_apiOperationMutex; 77 static int64_t g_processorId; 78 static const int64_t NULLPTR_PROCCESSORID = 0; 79 std::unordered_map<std::string, std::shared_ptr<EventPeriodExpandableData>> m_thresholdData; 80 }; 81 } // namespace Accessibility 82 } // namespace OHOS 83 #endif // ACCESSIBILITY_API_EVENT_REPORTER_H