1 /*
2  * Copyright (c) 2021-2024 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 "json/json.h"
28 #include "sys_event.h"
29 
30 namespace OHOS {
31 namespace HiviewDFX {
32 constexpr uint8_t INVALID_EVENT_TYPE = 0;
33 constexpr uint8_t DEFAULT_PRIVACY = 4;
34 
35 struct BaseInfo {
36     uint8_t type = INVALID_EVENT_TYPE;
37     uint8_t privacy = DEFAULT_PRIVACY;
38     std::string level;
39     std::string tag;
40     bool preserve = true;
41 };
42 using NAME_INFO_MAP = std::unordered_map<std::string, BaseInfo>;
43 using DOMAIN_INFO_MAP = std::unordered_map<std::string, NAME_INFO_MAP>;
44 using JSON_VALUE_LOOP_HANDLER = std::function<void(const std::string&, const Json::Value&)>;
45 
46 class EventJsonParser {
47 public:
48     EventJsonParser(const std::string& defFilePath);
~EventJsonParser()49     ~EventJsonParser() {};
50 
51 public:
52     std::string GetTagByDomainAndName(const std::string& domain, const std::string& name) const;
53     int GetTypeByDomainAndName(const std::string& domain, const std::string& name) const;
54     bool GetPreserveByDomainAndName(const std::string& domain, const std::string& name) const;
55     void ReadDefFile(const std::string& defFilePath);
56     BaseInfo GetDefinedBaseInfoByDomainName(const std::string& domain, const std::string& name) const;
57 
58 private:
59     bool HasIntMember(const Json::Value& jsonObj, const std::string& name) const;
60     bool HasStringMember(const Json::Value& jsonObj, const std::string& name) const;
61     bool HasBoolMember(const Json::Value& jsonObj, const std::string& name) const;
62     void InitEventInfoMapRef(const Json::Value& jsonObj, JSON_VALUE_LOOP_HANDLER handler) const;
63     BaseInfo ParseBaseConfig(const Json::Value& eventNameJson) const;
64     void ParseHiSysEventDef(const Json::Value& hiSysEventDef, std::shared_ptr<DOMAIN_INFO_MAP> sysDefMap);
65     NAME_INFO_MAP ParseNameConfig(const Json::Value& domainJson) const;
66     void WatchTestTypeParameter();
67 
68 private:
69     std::shared_ptr<DOMAIN_INFO_MAP> hiSysEventDefMap_ = nullptr;
70 }; // EventJsonParser
71 } // namespace HiviewDFX
72 } // namespace OHOS
73 #endif
74