1 /*
2  * Copyright (c) 2023 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 FOUNDATION_ACE_FRAMEWORKS_CORE_RECORDER_EVENT_CONFIG_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_RECORDER_EVENT_CONFIG_H
18 
19 #include <string>
20 #include <list>
21 #include <memory>
22 #include <unordered_map>
23 
24 #include "base/json/json_util.h"
25 
26 namespace OHOS::Ace::Recorder {
27 enum class EventCategory {
28     CATEGORY_PAGE,
29     CATEGORY_COMPONENT,
30     CATEGORY_EXPOSURE,
31     CATEGORY_PAGE_PARAM,
32 };
33 
34 struct ExposureCfg {
35     std::string id;
36     double ratio = 0.0;
37     int duration = 0;
38 
39     bool operator==(const ExposureCfg& exposureCfg) const
40     {
41         return id == exposureCfg.id;
42     }
43 };
44 
45 struct ExposureCfgHash {
operatorExposureCfgHash46     size_t operator()(const ExposureCfg& exposureCfg) const
47     {
48         return std::hash<std::string>()(exposureCfg.id);
49     }
50 };
51 
52 struct PageCfg {
53     std::list<std::string> shareNodes;
54     std::list<ExposureCfg> exposureCfgs;
55 };
56 
57 using Config = std::unordered_map<std::string, PageCfg>;
58 using Switch = std::unordered_map<EventCategory, bool>;
59 
60 class EventConfig final {
61 public:
62     EventConfig();
63     ~EventConfig() = default;
64 
65     void Init(const std::string& config);
66     bool IsEnable() const;
67     bool IsCategoryEnable(EventCategory category) const;
68     const std::shared_ptr<Config>& GetConfig() const;
69 
70 private:
71     inline void ParseSwitch(const std::unique_ptr<JsonValue>& jsonObj);
72     inline void ParseShareNode(const std::unique_ptr<JsonValue>& shareNodeArray, PageCfg& pageCfg);
73     inline void ParseExposureCfg(const std::unique_ptr<JsonValue>& exposureCfgArray, PageCfg& pageCfg);
74     bool enable_ = false;
75     std::shared_ptr<Switch> switches_;
76 
77     std::shared_ptr<Config> config_;
78 };
79 } // namespace OHOS::Ace::Recorder
80 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_RECORDER_EVENT_CONFIG_H
81