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 #include "core/common/recorder/event_config.h"
16 
17 #include "core/common/recorder/event_recorder.h"
18 
19 namespace OHOS::Ace::Recorder {
EventConfig()20 EventConfig::EventConfig()
21 {
22     switches_ = std::make_shared<Switch>();
23     config_ = std::make_shared<Config>();
24 }
25 
Init(const std::string & config)26 void EventConfig::Init(const std::string& config)
27 {
28     auto jsonObj = JsonUtil::ParseJsonString(config);
29     if (!jsonObj || !jsonObj->IsValid() || !jsonObj->IsObject()) {
30         return;
31     }
32     ParseSwitch(jsonObj);
33     auto cfgJsonArray = jsonObj->GetValue("config");
34     if (!cfgJsonArray || !cfgJsonArray->IsArray()) {
35         return;
36     }
37     for (int32_t i = 0; i < cfgJsonArray->GetArraySize(); ++i) {
38         auto item = cfgJsonArray->GetArrayItem(i);
39         if (!item || !item->IsObject()) {
40             continue;
41         }
42         auto pageUrl = item->GetString("pageUrl");
43         if (pageUrl.empty()) {
44             continue;
45         }
46         PageCfg pageCfg;
47         auto shareNodeArray = item->GetValue("shareNode");
48         if (shareNodeArray && shareNodeArray->IsArray()) {
49             ParseShareNode(shareNodeArray, pageCfg);
50         }
51 
52         auto exposureCfgArray = item->GetValue("exposureCfg");
53         if (exposureCfgArray && exposureCfgArray->IsArray()) {
54             ParseExposureCfg(exposureCfgArray, pageCfg);
55         }
56         config_->emplace(std::move(pageUrl), std::move(pageCfg));
57     }
58 }
59 
ParseSwitch(const std::unique_ptr<JsonValue> & jsonObj)60 void EventConfig::ParseSwitch(const std::unique_ptr<JsonValue>& jsonObj)
61 {
62     enable_ = jsonObj->GetBool("enable", false);
63     auto switchVal = jsonObj->GetValue("switch");
64     if (switchVal && switchVal->IsObject()) {
65         switches_->emplace(EventCategory::CATEGORY_PAGE, switchVal->GetBool("page", false));
66         switches_->emplace(EventCategory::CATEGORY_COMPONENT, switchVal->GetBool("component", false));
67         switches_->emplace(EventCategory::CATEGORY_EXPOSURE, switchVal->GetBool("exposure", false));
68         switches_->emplace(EventCategory::CATEGORY_PAGE_PARAM, switchVal->GetBool("pageParam", false));
69     }
70     auto globalSwitchVal = jsonObj->GetValue("globalSwitch");
71     if (globalSwitchVal && globalSwitchVal->IsObject()) {
72         EventRecorder::Get().pageEnable_ = globalSwitchVal->GetBool("page", true);
73         EventRecorder::Get().componentEnable_ = globalSwitchVal->GetBool("component", true);
74         EventRecorder::Get().exposureEnable_ = globalSwitchVal->GetBool("exposure", true);
75         EventRecorder::Get().pageParamEnable_ = globalSwitchVal->GetBool("pageParam", true);
76     }
77 }
78 
ParseShareNode(const std::unique_ptr<JsonValue> & shareNodeArray,PageCfg & pageCfg)79 void EventConfig::ParseShareNode(const std::unique_ptr<JsonValue>& shareNodeArray, PageCfg& pageCfg)
80 {
81     for (int32_t j = 0; j < shareNodeArray->GetArraySize(); ++j) {
82         auto shareNodeItem = shareNodeArray->GetArrayItem(j);
83         if (!shareNodeItem || !shareNodeItem->IsString()) {
84             continue;
85         }
86         auto id = shareNodeItem->GetString();
87         if (id.empty()) {
88             continue;
89         }
90         pageCfg.shareNodes.emplace_back(std::move(id));
91     }
92 }
93 
ParseExposureCfg(const std::unique_ptr<JsonValue> & exposureCfgArray,PageCfg & pageCfg)94 void EventConfig::ParseExposureCfg(const std::unique_ptr<JsonValue>& exposureCfgArray, PageCfg& pageCfg)
95 {
96     for (int32_t j = 0; j < exposureCfgArray->GetArraySize(); ++j) {
97         auto exposureCfgJson = exposureCfgArray->GetArrayItem(j);
98         if (!exposureCfgJson || !exposureCfgJson->IsObject()) {
99             continue;
100         }
101         auto id = exposureCfgJson->GetString("id");
102         auto ratio = exposureCfgJson->GetDouble("ratio");
103         auto duration = exposureCfgJson->GetInt("duration");
104         if (id.empty() || duration <= 0) {
105             continue;
106         }
107         ExposureCfg exposureCfg { std::move(id), ratio, duration };
108         pageCfg.exposureCfgs.emplace_back(std::move(exposureCfg));
109     }
110 }
111 
IsEnable() const112 bool EventConfig::IsEnable() const
113 {
114     return enable_;
115 }
116 
IsCategoryEnable(EventCategory category) const117 bool EventConfig::IsCategoryEnable(EventCategory category) const
118 {
119     auto iter = switches_->find(category);
120     if (iter == switches_->end()) {
121         return false;
122     }
123     return iter->second;
124 }
125 
GetConfig() const126 const std::shared_ptr<Config>& EventConfig::GetConfig() const
127 {
128     return config_;
129 }
130 } // namespace OHOS::Ace::Recorder
131