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