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