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,bool defaultValue=false)63 void FillSwitch(std::vector<bool>& switches, const std::unique_ptr<JsonValue>& jsonObj, bool defaultValue = false)
64 {
65 switches[static_cast<int32_t>(EventCategory::CATEGORY_PAGE)] = jsonObj->GetBool("page", defaultValue);
66 switches[static_cast<int32_t>(EventCategory::CATEGORY_COMPONENT)] = jsonObj->GetBool("component", defaultValue);
67 switches[static_cast<int32_t>(EventCategory::CATEGORY_EXPOSURE)] = jsonObj->GetBool("exposure", defaultValue);
68 switches[static_cast<int32_t>(EventCategory::CATEGORY_PAGE_PARAM)] = jsonObj->GetBool("pageParam", defaultValue);
69 switches[static_cast<int32_t>(EventCategory::CATEGORY_SCROLL)] = jsonObj->GetBool("scroll", defaultValue);
70 switches[static_cast<int32_t>(EventCategory::CATEGORY_ANIMATION)] = jsonObj->GetBool("animation", defaultValue);
71 switches[static_cast<int32_t>(EventCategory::CATEGORY_RECT)] = jsonObj->GetBool("rect", defaultValue);
72 switches[static_cast<int32_t>(EventCategory::CATEGORY_WEB)] = jsonObj->GetBool("web", defaultValue);
73 switches[static_cast<int32_t>(EventCategory::CATEGORY_TEXT_INPUT)] = jsonObj->GetBool("textInput", defaultValue);
74 switches[static_cast<int32_t>(EventCategory::CATEGORY_POINT)] = jsonObj->GetBool("point", defaultValue);
75 switches[static_cast<int32_t>(EventCategory::CATEGORY_PARENT_TEXT)] = jsonObj->GetBool("parentText", defaultValue);
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 std::vector<bool> globalSwitch(static_cast<int32_t>(EventCategory::CATEGORY_END), true);
88 FillSwitch(globalSwitch, globalSwitchVal, true);
89 EventRecorder::Get().UpdateGlobalEventSwitch(globalSwitch);
90 }
91 }
92
ParseJsCode(const std::unique_ptr<JsonValue> & jsonObj)93 void EventConfig::ParseJsCode(const std::unique_ptr<JsonValue>& jsonObj)
94 {
95 webCategory_ = jsonObj->GetString("webCategory");
96 webIdentifier_ = jsonObj->GetString("webIdentifier");
97 webJsCode_ = jsonObj->GetString("webActionJs");
98 }
99
ParseShareNode(const std::unique_ptr<JsonValue> & shareNodeArray,PageCfg & pageCfg)100 void EventConfig::ParseShareNode(const std::unique_ptr<JsonValue>& shareNodeArray, PageCfg& pageCfg)
101 {
102 for (int32_t j = 0; j < shareNodeArray->GetArraySize(); ++j) {
103 auto shareNodeItem = shareNodeArray->GetArrayItem(j);
104 if (!shareNodeItem || !shareNodeItem->IsString()) {
105 continue;
106 }
107 auto id = shareNodeItem->GetString();
108 if (id.empty()) {
109 continue;
110 }
111 pageCfg.shareNodes.emplace_back(std::move(id));
112 }
113 }
114
ParseExposureCfg(const std::unique_ptr<JsonValue> & exposureCfgArray,PageCfg & pageCfg)115 void EventConfig::ParseExposureCfg(const std::unique_ptr<JsonValue>& exposureCfgArray, PageCfg& pageCfg)
116 {
117 for (int32_t j = 0; j < exposureCfgArray->GetArraySize(); ++j) {
118 auto exposureCfgJson = exposureCfgArray->GetArrayItem(j);
119 if (!exposureCfgJson || !exposureCfgJson->IsObject()) {
120 continue;
121 }
122 auto id = exposureCfgJson->GetString("id");
123 auto ratio = exposureCfgJson->GetDouble("ratio");
124 auto duration = exposureCfgJson->GetInt("duration");
125 if (id.empty() || duration <= 0) {
126 continue;
127 }
128 ExposureCfg exposureCfg { std::move(id), ratio, duration };
129 pageCfg.exposureCfgs.emplace_back(std::move(exposureCfg));
130 }
131 }
132
IsEnable() const133 bool EventConfig::IsEnable() const
134 {
135 return enable_;
136 }
137
IsCategoryEnable(int32_t index) const138 bool EventConfig::IsCategoryEnable(int32_t index) const
139 {
140 if (index < 0 || index >= static_cast<int32_t>(switches_.size())) {
141 return false;
142 }
143 return switches_[index];
144 }
145
GetConfig() const146 const std::shared_ptr<Config>& EventConfig::GetConfig() const
147 {
148 return config_;
149 }
150 } // namespace OHOS::Ace::Recorder
151