• 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 
39 struct KeyConfig {
40     uint8_t type : 3;
41     uint8_t privacy : 3;
42     uint8_t preserve : 1;
43     uint8_t collect : 1;
44 
45     KeyConfig(uint8_t type = INVALID_EVENT_TYPE, uint8_t privacy = DEFAULT_PRIVACY,
46         uint8_t preserve = DEFAULT_PERSERVE_VAL, uint8_t collect = DEFAULT_COLLECT_VAL)
typeKeyConfig47         : type(type), privacy(privacy), preserve(preserve), collect(collect) {}
48 };
49 
50 struct BaseInfo {
51     KeyConfig keyConfig;
52     std::string level;
53     std::string tag;
54     PARAM_INFO_MAP_PTR disallowParams;
55 };
56 
57 using NAME_INFO_MAP = std::unordered_map<std::string, BaseInfo>;
58 using DOMAIN_INFO_MAP = std::unordered_map<std::string, NAME_INFO_MAP>;
59 using JSON_VALUE_LOOP_HANDLER = std::function<void(const std::string&, const Json::Value&)>;
60 using ExportEventList = std::map<std::string, std::vector<std::string>>; // <domain, names>
61 
62 class EventJsonParser : public DelayedSingleton<EventJsonParser> {
63 DECLARE_DELAYED_SINGLETON(EventJsonParser);
64 
65 public:
66     std::string GetTagByDomainAndName(const std::string& domain, const std::string& name);
67     uint8_t GetTypeByDomainAndName(const std::string& domain, const std::string& name);
68     bool GetPreserveByDomainAndName(const std::string& domain, const std::string& name);
69     void OnConfigUpdate();
70     BaseInfo GetDefinedBaseInfoByDomainName(const std::string& domain, const std::string& name);
71     void GetAllCollectEvents(ExportEventList& list);
72     void ReadDefFile();
73 
74 private:
75     bool HasUIntMember(const Json::Value& jsonObj, const std::string& name) const;
76     bool HasStringMember(const Json::Value& jsonObj, const std::string& name) const;
77     bool HasBoolMember(const Json::Value& jsonObj, const std::string& name) const;
78     void InitEventInfoMapRef(const Json::Value& jsonObj, JSON_VALUE_LOOP_HANDLER handler) const;
79     BaseInfo ParseBaseConfig(const Json::Value& eventNameJson) const;
80     void ParseSysEventDef(const Json::Value& hiSysEventDef, std::shared_ptr<DOMAIN_INFO_MAP> sysDefMap);
81     NAME_INFO_MAP ParseEventNameConfig(const std::string& domain, const Json::Value& domainJson) const;
82     PARAM_INFO_MAP_PTR ParseEventParamInfo(const Json::Value& eventContent) const;
83     void WatchTestTypeParameter();
84 
85 private:
86     mutable ffrt::mutex defMtx_;
87     std::shared_ptr<DOMAIN_INFO_MAP> sysEventDefMap_ = nullptr;
88 }; // EventJsonParser
89 } // namespace HiviewDFX
90 } // namespace OHOS
91 #endif
92