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 
26 namespace OHOS::Ace::Recorder {
27 enum EventType : int32_t {
28     INVALID = 0,
29     PAGE_SHOW,
30     PAGE_HIDE,
31     CLICK,
32     LONG_PRESS,
33     CHANGE,
34     EXPOSURE,
35     NAVIGATOR,
36     REFRESH,
37     STEPPER_FINISH,
38     STEPPER_SKIP,
39     STEPPER_NEXT,
40     STEPPER_PREVIOUS,
41     SEARCH_SUBMIT,
42     WEB_PAGE_BEGIN,
43     WEB_PAGE_END,
44     VIDEO_START,
45     VIDEO_PAUSE,
46     VIDEO_FINISH,
47     VIDEO_ERROR,
48     VIDEO_PREPARED,
49     VIDEO_SEEKED,
50     VIDEO_SCREEN_CHANGE,
51     VIDEO_STOP,
52     DIALOG_SHOW,
53     DIALOG_CANCEL,
54     DIALOG_ACTION,
55     DIALOG_SELECT,
56     DIALOG_ACCEPT,
57 };
58 
59 enum PageEventType: int32_t {
60     ROUTER_PAGE = 0,
61     NAV_PAGE,
62 };
63 
64 struct EventSwitch {
65     bool pageEnable = true;
66     bool exposureEnable = false;
67     bool componentEnable = false;
68     bool pageParamEnable = false;
69 };
70 
71 constexpr char KEY_ID[] = "id";
72 constexpr char KEY_TYPE[] = "type";
73 constexpr char KEY_NAV_DST[] = "navDst";
74 constexpr char KEY_PAGE[] = "page";
75 constexpr char KEY_DESCRIPTION[] = "description";
76 constexpr char KEY_PAGE_PARAM[] = "pageParam";
77 constexpr char KEY_DURATION[] = "duration";
78 constexpr char KEY_TEXT[] = "text";
79 constexpr char KEY_CHECKED[] = "checked";
80 constexpr char KEY_INDEX[] = "index";
81 constexpr char KEY_TEXT_ARRAY[] = "textArray";
82 constexpr char KEY_TITLE[] = "title";
83 constexpr char KEY_SUB_TITLE[] = "subtitle";
84 constexpr char KEY_NAV_PAGE[] = "navPage";
85 constexpr char KEY_NAV_PAGE_TYPE[] = "navType";
86 constexpr char KEY_NAV_PAGE_PARAM[] = "navPageParam";
87 
88 ACE_FORCE_EXPORT bool IsCacheAvaliable();
89 
90 class ACE_FORCE_EXPORT EventParamsBuilder {
91 public:
92     EventParamsBuilder();
93 
94     ~EventParamsBuilder() = default;
95 
96     EventParamsBuilder& SetEventType(EventType eventType);
97 
98     EventParamsBuilder& SetId(const std::string& id);
99 
100     EventParamsBuilder& SetType(const std::string& type);
101 
102     EventParamsBuilder& SetDescription(const std::string& desc);
103 
104     EventParamsBuilder& SetNavDst(const std::string& dstName);
105 
106     EventParamsBuilder& SetPageUrl(const std::string& pageUrl);
107 
108     EventParamsBuilder& SetText(const std::string& value);
109 
110     EventParamsBuilder& SetChecked(bool value);
111 
112     EventParamsBuilder& SetIndex(int value);
113 
114     EventParamsBuilder& SetTextArray(const std::vector<std::string>& value);
115 
116     EventParamsBuilder& SetExtra(const std::string& key, const std::string& value);
117 
118     std::shared_ptr<std::unordered_map<std::string, std::string>> build();
119 
120     EventType GetEventType() const;
121 
122     std::string GetText() const;
123 
124     std::string ToString() const;
125 
126 private:
127     std::shared_ptr<std::unordered_map<std::string, std::string>> params_;
128     EventType eventType_ = EventType::INVALID;
129 };
130 
131 std::string MapToString(const std::shared_ptr<std::unordered_map<std::string, std::string>>& input);
132 
133 class ACE_FORCE_EXPORT EventRecorder final {
134 public:
135     static EventRecorder& Get();
136 
137     bool IsPageRecordEnable() const;
138     bool IsPageParamRecordEnable() const;
139     bool IsExposureRecordEnable() const;
140     bool IsComponentRecordEnable() const;
141     void UpdateEventSwitch(const EventSwitch& eventSwitch);
142 
SetContainerChanged()143     void SetContainerChanged()
144     {
145         isFocusContainerChanged_ = true;
146     }
147 
148     void SetContainerInfo(const std::string& windowName, int32_t id, bool foreground);
149     void SetFocusContainerInfo(const std::string& windowName, int32_t id);
150     int32_t GetContainerId();
151     const std::string& GetPageUrl();
152     const std::string& GetNavDstName() const;
153 
154     void OnPageShow(const std::string& pageUrl, const std::string& param);
155     void OnPageHide(const std::string& pageUrl, const int64_t duration);
156     void OnClick(EventParamsBuilder&& builder);
157     void OnChange(EventParamsBuilder&& builder);
158     void OnEvent(EventParamsBuilder&& builder);
159     void OnNavDstShow(EventParamsBuilder&& builder);
160     void OnNavDstHide(EventParamsBuilder&& builder);
161     void OnExposure(EventParamsBuilder&& builder);
162 
163 private:
164     EventRecorder();
165     ~EventRecorder() = default;
166     friend class EventConfig;
167 
168     EventSwitch eventSwitch_;
169 
170     bool pageEnable_ = true;
171     bool pageParamEnable_ = false;
172     bool componentEnable_ = true;
173     bool exposureEnable_ = true;
174 
175     int32_t containerId_ = -1;
176     int32_t focusContainerId_ = -1;
177     int32_t containerCount_ = 0;
178 
179     std::string pageUrl_;
180     std::string navDstName_;
181     int64_t navShowTime_ = -1;
182     bool isFocusContainerChanged_ = false;
183 
184     RefPtr<TaskExecutor> taskExecutor_;
185 
186     ACE_DISALLOW_COPY_AND_MOVE(EventRecorder);
187 };
188 } // namespace OHOS::Ace::Recorder
189 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_RECORDER_EVENT_RECORDER_H
190