• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <cstdint>
17 #include <vector>
18 
19 #include "core/common/recorder/event_recorder.h"
20 
21 namespace OHOS::Ace::Recorder {
EventConfig()22 EventConfig::EventConfig()
23 {
24     switches_.resize(static_cast<int32_t>(EventCategory::CATEGORY_END), false);
25     config_ = std::make_shared<Config>();
26 }
27 
Init(const std::string & config)28 void EventConfig::Init(const std::string& config)
29 {
30     auto jsonObj = JsonUtil::ParseJsonString(config);
31     if (!jsonObj || !jsonObj->IsValid() || !jsonObj->IsObject()) {
32         return;
33     }
34     ParseSwitch(jsonObj);
35     ParseJsCode(jsonObj);
36     auto cfgJsonArray = jsonObj->GetValue("config");
37     if (!cfgJsonArray || !cfgJsonArray->IsArray()) {
38         return;
39     }
40     for (int32_t i = 0; i < cfgJsonArray->GetArraySize(); ++i) {
41         auto item = cfgJsonArray->GetArrayItem(i);
42         if (!item || !item->IsObject()) {
43             continue;
44         }
45         auto pageUrl = item->GetString("pageUrl");
46         if (pageUrl.empty()) {
47             continue;
48         }
49         PageCfg pageCfg;
50         auto shareNodeArray = item->GetValue("shareNode");
51         if (shareNodeArray && shareNodeArray->IsArray()) {
52             ParseShareNode(shareNodeArray, pageCfg);
53         }
54 
55         auto exposureCfgArray = item->GetValue("exposureCfg");
56         if (exposureCfgArray && exposureCfgArray->IsArray()) {
57             ParseExposureCfg(exposureCfgArray, pageCfg);
58         }
59         config_->emplace(std::move(pageUrl), std::move(pageCfg));
60     }
61 }
62 
FillSwitch(std::vector<bool> & switches,const std::unique_ptr<JsonValue> & jsonObj)63 void FillSwitch(std::vector<bool>& switches, const std::unique_ptr<JsonValue>& jsonObj)
64 {
65     switches[static_cast<int32_t>(EventCategory::CATEGORY_PAGE)] = jsonObj->GetBool("page", false);
66     switches[static_cast<int32_t>(EventCategory::CATEGORY_COMPONENT)] = jsonObj->GetBool("component", false);
67     switches[static_cast<int32_t>(EventCategory::CATEGORY_EXPOSURE)] = jsonObj->GetBool("exposure", false);
68     switches[static_cast<int32_t>(EventCategory::CATEGORY_PAGE_PARAM)] = jsonObj->GetBool("pageParam", false);
69     switches[static_cast<int32_t>(EventCategory::CATEGORY_SCROLL)] = jsonObj->GetBool("scroll", false);
70     switches[static_cast<int32_t>(EventCategory::CATEGORY_ANIMATION)] = jsonObj->GetBool("animation", false);
71     switches[static_cast<int32_t>(EventCategory::CATEGORY_RECT)] = jsonObj->GetBool("rect", false);
72     switches[static_cast<int32_t>(EventCategory::CATEGORY_WEB)] = jsonObj->GetBool("web", false);
73     switches[static_cast<int32_t>(EventCategory::CATEGORY_TEXT_INPUT)] = jsonObj->GetBool("textInput", false);
74     switches[static_cast<int32_t>(EventCategory::CATEGORY_POINT)] = jsonObj->GetBool("point", false);
75     switches[static_cast<int32_t>(EventCategory::CATEGORY_PARENT_TEXT)] = jsonObj->GetBool("parentText", false);
76 }
77 
ParseSwitch(const std::unique_ptr<JsonValue> & jsonObj)78 void EventConfig::ParseSwitch(const std::unique_ptr<JsonValue>& jsonObj)
79 {
80     enable_ = jsonObj->GetBool("enable", false);
81     auto switchVal = jsonObj->GetValue("switch");
82     if (switchVal && switchVal->IsObject()) {
83         FillSwitch(switches_, switchVal);
84     }
85     auto globalSwitchVal = jsonObj->GetValue("globalSwitch");
86     if (globalSwitchVal && globalSwitchVal->IsObject()) {
87         FillSwitch(EventRecorder::Get().globalSwitch_, globalSwitchVal);
88     }
89 }
90 
ParseJsCode(const std::unique_ptr<JsonValue> & jsonObj)91 void EventConfig::ParseJsCode(const std::unique_ptr<JsonValue>& jsonObj)
92 {
93     webCategory_ = jsonObj->GetString("webCategory");
94     webIdentifier_ = jsonObj->GetString("webIdentifier");
95     webJsCode_ = jsonObj->GetString("webActionJs");
96 }
97 
ParseShareNode(const std::unique_ptr<JsonValue> & shareNodeArray,PageCfg & pageCfg)98 void EventConfig::ParseShareNode(const std::unique_ptr<JsonValue>& shareNodeArray, PageCfg& pageCfg)
99 {
100     for (int32_t j = 0; j < shareNodeArray->GetArraySize(); ++j) {
101         auto shareNodeItem = shareNodeArray->GetArrayItem(j);
102         if (!shareNodeItem || !shareNodeItem->IsString()) {
103             continue;
104         }
105         auto id = shareNodeItem->GetString();
106         if (id.empty()) {
107             continue;
108         }
109         pageCfg.shareNodes.emplace_back(std::move(id));
110     }
111 }
112 
ParseExposureCfg(const std::unique_ptr<JsonValue> & exposureCfgArray,PageCfg & pageCfg)113 void EventConfig::ParseExposureCfg(const std::unique_ptr<JsonValue>& exposureCfgArray, PageCfg& pageCfg)
114 {
115     for (int32_t j = 0; j < exposureCfgArray->GetArraySize(); ++j) {
116         auto exposureCfgJson = exposureCfgArray->GetArrayItem(j);
117         if (!exposureCfgJson || !exposureCfgJson->IsObject()) {
118             continue;
119         }
120         auto id = exposureCfgJson->GetString("id");
121         auto ratio = exposureCfgJson->GetDouble("ratio");
122         auto duration = exposureCfgJson->GetInt("duration");
123         if (id.empty() || duration <= 0) {
124             continue;
125         }
126         ExposureCfg exposureCfg { std::move(id), ratio, duration };
127         pageCfg.exposureCfgs.emplace_back(std::move(exposureCfg));
128     }
129 }
130 
IsEnable() const131 bool EventConfig::IsEnable() const
132 {
133     return enable_;
134 }
135 
IsCategoryEnable(int32_t index) const136 bool EventConfig::IsCategoryEnable(int32_t index) const
137 {
138     if (index < 0 || index >= static_cast<int32_t>(switches_.size())) {
139         return false;
140     }
141     return switches_[index];
142 }
143 
GetConfig() const144 const std::shared_ptr<Config>& EventConfig::GetConfig() const
145 {
146     return config_;
147 }
148 } // namespace OHOS::Ace::Recorder
149