• 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 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_RECORDER_EVENT_RECORDER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_RECORDER_EVENT_RECORDER_H
18 
19 #include <string>
20 #include <unordered_map>
21 
22 #include "base/thread/task_executor.h"
23 #include "base/utils/noncopyable.h"
24 #include "core/common/recorder/event_config.h"
25 #include "core/common/recorder/event_definition.h"
26 #include "core/components_ng/base/frame_node.h"
27 
28 namespace OHOS::Ace::Recorder {
29 enum PageEventType: int32_t {
30     ROUTER_PAGE = 0,
31     NAV_PAGE,
32 };
33 
34 constexpr char KEY_ID[] = "id";
35 constexpr char KEY_TYPE[] = "type";
36 constexpr char KEY_NAV_DST[] = "navDst";
37 constexpr char KEY_PAGE[] = "page";
38 constexpr char KEY_DESCRIPTION[] = "description";
39 constexpr char KEY_PAGE_PARAM[] = "pageParam";
40 constexpr char KEY_DURATION[] = "duration";
41 constexpr char KEY_TEXT[] = "text";
42 constexpr char KEY_CHECKED[] = "checked";
43 constexpr char KEY_INDEX[] = "index";
44 constexpr char KEY_TEXT_ARRAY[] = "textArray";
45 constexpr char KEY_TITLE[] = "title";
46 constexpr char KEY_SUB_TITLE[] = "subtitle";
47 constexpr char KEY_NAV_PAGE[] = "navPage";
48 constexpr char KEY_NAV_PAGE_TYPE[] = "navType";
49 constexpr char KEY_NAV_PAGE_PARAM[] = "navPageParam";
50 
51 using WebJsItem = std::map<std::string, std::vector<std::string>>;
52 
53 ACE_FORCE_EXPORT bool IsCacheAvaliable();
54 
55 class ACE_FORCE_EXPORT EventParamsBuilder {
56 public:
57     EventParamsBuilder();
58 
59     ~EventParamsBuilder() = default;
60 
61     EventParamsBuilder& SetEventType(EventType eventType);
62 
63     EventParamsBuilder& SetEventCategory(EventCategory category);
64 
65     EventParamsBuilder& SetId(const std::string& id);
66 
67     EventParamsBuilder& SetType(const std::string& type);
68 
69     EventParamsBuilder& SetDescription(const std::string& desc);
70 
71     EventParamsBuilder& SetNavDst(const std::string& dstName);
72 
73     EventParamsBuilder& SetPageUrl(const std::string& pageUrl);
74 
75     EventParamsBuilder& SetText(const std::string& value);
76 
77     EventParamsBuilder& SetChecked(bool value);
78 
79     EventParamsBuilder& SetIndex(int value);
80 
81     EventParamsBuilder& SetTextArray(const std::vector<std::string>& value);
82 
83     EventParamsBuilder& SetHost(const RefPtr<NG::FrameNode>& node);
84 
85     EventParamsBuilder& SetExtra(const std::string& key, const std::string& value);
86 
87     std::shared_ptr<std::unordered_map<std::string, std::string>> build();
88 
89     EventType GetEventType() const;
90 
91     EventCategory GetEventCategory() const;
92 
93     std::string GetValue(const std::string& key) const;
94 
95     std::string ToString() const;
96 
97 private:
98     std::shared_ptr<std::unordered_map<std::string, std::string>> params_;
99     EventType eventType_ = EventType::INVALID;
100     EventCategory category_ = EventCategory::CATEGORY_COMPONENT;
101 };
102 
103 std::string MapToString(const std::shared_ptr<std::unordered_map<std::string, std::string>>& input);
104 
105 class ACE_FORCE_EXPORT EventRecorder final {
106 public:
107     static EventRecorder& Get();
108 
109     bool IsPageRecordEnable() const;
110     bool IsPageParamRecordEnable() const;
111     bool IsExposureRecordEnable() const;
112     bool IsComponentRecordEnable() const;
113     bool IsRecordEnable(EventCategory category) const;
114     void UpdateEventSwitch(const std::vector<bool>& eventSwitch);
115     void UpdateWebIdentifier(const std::unordered_map<std::string, std::string> identifierMap);
116 
117     void SetContainerInfo(const std::string& windowName, int32_t id, bool foreground);
118     void SetFocusContainerInfo(const std::string& windowName, int32_t id);
119     int32_t GetContainerId(bool isFoucs = true);
120     const std::string& GetPageUrl();
121     const std::string& GetNavDstName() const;
122     std::string GetCacheJsCode() const;
123     void FillWebJsCode(std::optional<WebJsItem>& scriptItems) const;
124     void SaveJavascriptItems(const WebJsItem& scriptItems, const std::vector<std::string>& orderScriptItems = {});
125     void HandleJavascriptItems(
126         std::optional<WebJsItem>& scriptItems, std::optional<std::vector<std::string>>& orderScriptItems);
127     bool IsMessageValid(const std::string& webCategory, const std::string& identifier);
128     void NotifyEventCacheEnd();
129 
130     void OnPageShow(const std::string& pageUrl, const std::string& param, const std::string& name = "");
131     void OnPageHide(const std::string& pageUrl, const int64_t duration, const std::string& name = "");
132     void OnClick(EventParamsBuilder&& builder);
133     void OnChange(EventParamsBuilder&& builder);
134     void OnEvent(EventParamsBuilder&& builder);
135     void OnNavDstShow(EventParamsBuilder&& builder);
136     void OnNavDstHide(EventParamsBuilder&& builder);
137     void OnExposure(EventParamsBuilder&& builder);
138     void OnWebEvent(const RefPtr<NG::FrameNode>& node, const std::vector<std::string>& params);
139     void OnAttachWeb(const RefPtr<NG::FrameNode>& node);
140     void OnDetachWeb(int32_t nodeId);
GetWeakNodeMap()141     std::unordered_map<int32_t, WeakPtr<NG::FrameNode>>& GetWeakNodeMap()
142     {
143         return weakNodeCache_;
144     }
145 
146 private:
147     EventRecorder();
148     ~EventRecorder() = default;
149     friend class EventConfig;
150 
151     std::vector<bool> eventSwitch_;
152     std::vector<bool> globalSwitch_;
153 
154     int32_t containerId_ = -1;
155     int32_t focusContainerId_ = -1;
156 
157     std::string pageUrl_;
158     std::string navDstName_;
159     int64_t navShowTime_ = -1;
160     bool isFocusContainerChanged_ = false;
161 
162     RefPtr<TaskExecutor> taskExecutor_;
163 
164     std::string jsCode_;
165     std::optional<std::map<std::string, std::vector<std::string>>> cacheScriptItems_;
166     std::optional<std::vector<std::string>> cacheOrderScriptItems_;
167     std::unordered_map<std::string, std::string> webIdentifierMap_;
168 
169     std::unordered_map<int32_t, WeakPtr<NG::FrameNode>> weakNodeCache_;
170 
171     ACE_DISALLOW_COPY_AND_MOVE(EventRecorder);
172 };
173 } // namespace OHOS::Ace::Recorder
174 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_RECORDER_EVENT_RECORDER_H
175