• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_EVENT_JSON_PARSER_H
17 #define HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_EVENT_JSON_PARSER_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <list>
22 #include <memory>
23 #include <string>
24 #include <unordered_map>
25 #include <vector>
26 
27 #include "ffrt.h"
28 #include "json/json.h"
29 #include "singleton.h"
30 #include "sys_event.h"
31 
32 namespace OHOS {
33 namespace HiviewDFX {
34 constexpr uint8_t INVALID_EVENT_TYPE = 0;
35 constexpr uint8_t DEFAULT_PRIVACY = 4;
36 constexpr uint8_t DEFAULT_PERSERVE_VAL = 1;
37 constexpr uint8_t DEFAULT_COLLECT_VAL = 0;
38 constexpr int16_t DEFAULT_REPORT_INTERVAL = 0;
39 
40 struct KeyConfig {
41     uint8_t type : 3;
42     uint8_t privacy : 3;
43     uint8_t preserve : 1;
44     uint8_t collect : 1;
45 
46     KeyConfig(uint8_t type = INVALID_EVENT_TYPE, uint8_t privacy = DEFAULT_PRIVACY,
47         uint8_t preserve = DEFAULT_PERSERVE_VAL, uint8_t collect = DEFAULT_COLLECT_VAL)
typeKeyConfig48         : type(type), privacy(privacy), preserve(preserve), collect(collect) {}
49 };
50 
51 struct BaseInfo {
52     KeyConfig keyConfig;
53     std::string level;
54     std::string tag;
55     PARAM_INFO_MAP_PTR disallowParams;
56     int16_t reportInterval = DEFAULT_REPORT_INTERVAL;
57 };
58 
59 using NAME_INFO_MAP = std::unordered_map<std::string, BaseInfo>;
60 using DOMAIN_INFO_MAP = std::unordered_map<std::string, NAME_INFO_MAP>;
61 using JSON_VALUE_LOOP_HANDLER = std::function<void(const std::string&, const Json::Value&)>;
62 using ExportEventList = std::map<std::string, std::vector<std::string>>; // <domain, names>
63 
64 class EventJsonParser : public DelayedSingleton<EventJsonParser> {
65 DECLARE_DELAYED_SINGLETON(EventJsonParser);
66 
67 public:
68     std::string GetTagByDomainAndName(const std::string& domain, const std::string& name);
69     uint8_t GetTypeByDomainAndName(const std::string& domain, const std::string& name);
70     bool GetPreserveByDomainAndName(const std::string& domain, const std::string& name);
71     void OnConfigUpdate();
72     BaseInfo GetDefinedBaseInfoByDomainName(const std::string& domain, const std::string& name);
73     void GetAllCollectEvents(ExportEventList& list);
74     void ReadDefFile();
75 
76 private:
77     bool HasIntMember(const Json::Value& jsonObj, const std::string& name) const;
78     bool HasUIntMember(const Json::Value& jsonObj, const std::string& name) const;
79     bool HasStringMember(const Json::Value& jsonObj, const std::string& name) const;
80     bool HasBoolMember(const Json::Value& jsonObj, const std::string& name) const;
81     void InitEventInfoMapRef(const Json::Value& jsonObj, JSON_VALUE_LOOP_HANDLER handler) const;
82     BaseInfo ParseBaseConfig(const Json::Value& eventNameJson) const;
83     void ParseSysEventDef(const Json::Value& hiSysEventDef, std::shared_ptr<DOMAIN_INFO_MAP> sysDefMap);
84     NAME_INFO_MAP ParseEventNameConfig(const std::string& domain, const Json::Value& domainJson) const;
85     PARAM_INFO_MAP_PTR ParseEventParamInfo(const Json::Value& eventContent) const;
86     void WatchTestTypeParameter();
87 
88 private:
89     mutable ffrt::mutex defMtx_;
90     std::shared_ptr<DOMAIN_INFO_MAP> sysEventDefMap_ = nullptr;
91 }; // EventJsonParser
92 } // namespace HiviewDFX
93 } // namespace OHOS
94 #endif
95