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_DATA_CACHE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_RECORDER_DATA_CACHE_H 18 19 #include <list> 20 #include <memory> 21 #include <shared_mutex> 22 #include <string> 23 #include <unordered_map> 24 #include <unordered_set> 25 #include <vector> 26 27 #include "base/utils/noncopyable.h" 28 #include "core/common/recorder/event_config.h" 29 #include "core/components_ng/base/frame_node.h" 30 31 namespace OHOS::Ace::Recorder { 32 constexpr int32_t MAX_SIZE_PER_PAGE = 50; 33 34 constexpr int32_t MAX_DATA_LENGTH = 100; 35 36 std::string GetPageUrlByNode(const RefPtr<NG::FrameNode>& node); 37 38 const std::string GetCurrentPageUrl(); 39 40 using NodeDataContainer = std::unordered_map<std::string, std::unordered_map<std::string, std::string>>; 41 42 struct MergedConfig { 43 std::unordered_map<std::string, std::unordered_set<std::string>> shareNodes; 44 std::unordered_map<std::string, std::unordered_set<ExposureCfg, ExposureCfgHash>> exposureNodes; 45 }; 46 47 class NodeDataCache final { 48 public: 49 static NodeDataCache& Get(); 50 51 void OnPageReady(); 52 53 void OnPageShow(const std::string& pageUrl); 54 55 void OnBeforePagePop(bool destroy = false); 56 57 void UpdateConfig(std::shared_ptr<MergedConfig>&& mergedConfig); 58 59 bool PutString(const RefPtr<NG::FrameNode>& node, const std::string& id, const std::string& value); 60 61 bool PutBool(const RefPtr<NG::FrameNode>& node, const std::string& id, bool value); 62 63 bool PutInt(const RefPtr<NG::FrameNode>& node, const std::string& id, int value); 64 65 bool PutStringArray( 66 const RefPtr<NG::FrameNode>& node, const std::string& id, const std::vector<std::string>& value); 67 68 bool PutMultiple(const RefPtr<NG::FrameNode>& node, const std::string& id, const std::string& name, bool value); 69 70 bool PutMultiple(const RefPtr<NG::FrameNode>& node, const std::string& id, const std::string& name, int index); 71 72 bool PutMultiple(const RefPtr<NG::FrameNode>& node, const std::string& id, const std::string& name, 73 const std::vector<std::string>& value); 74 75 void GetNodeData(const std::string& pageUrl, std::unordered_map<std::string, std::string>& nodes); 76 77 void Clear(const std::string& pageUrl); 78 79 void GetExposureCfg(const std::string& pageUrl, const std::string& inspectId, ExposureCfg& cfg); 80 ShouldCollectData()81 bool ShouldCollectData() const 82 { 83 return shouldCollectFull_; 84 } 85 IsShareNodeEmpty()86 bool IsShareNodeEmpty() const 87 { 88 return mergedConfig_->shareNodes.empty(); 89 } 90 91 private: 92 NodeDataCache(); 93 ~NodeDataCache() = default; 94 95 void Reset(); 96 97 std::shared_ptr<NodeDataContainer> container_; 98 99 std::shared_ptr<MergedConfig> mergedConfig_; 100 101 std::shared_mutex configMutex_; 102 103 std::shared_mutex cacheMutex_; 104 105 std::string pageUrl_; 106 107 std::string prePageUrl_; 108 109 bool shouldCollectFull_ = true; 110 111 ACE_DISALLOW_COPY_AND_MOVE(NodeDataCache); 112 }; 113 } // namespace OHOS::Ace::Recorder 114 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_RECORDER_DATA_CACHE_H 115