• 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_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 std::string GetPageUrlByContainerId(const int32_t containerId);
39 
40 std::string GetCurrentPageUrl();
41 
42 using NodeDataContainer = std::unordered_map<std::string, std::unordered_map<std::string, std::string>>;
43 
44 struct MergedConfig {
45     std::unordered_map<std::string, std::unordered_set<std::string>> shareNodes;
46     std::unordered_map<std::string, std::unordered_set<ExposureCfg, ExposureCfgHash>> exposureNodes;
47 };
48 
49 class NodeDataCache final {
50 public:
51     static NodeDataCache& Get();
52 
53     void OnPageReady();
54 
55     void OnPageShow(const std::string& pageUrl);
56 
57     void OnBeforePagePop(bool destroy = false);
58 
59     void UpdateConfig(std::shared_ptr<MergedConfig>&& mergedConfig);
60 
61     bool PutString(const RefPtr<NG::FrameNode>& node, const std::string& id, const std::string& value);
62 
63     bool PutBool(const RefPtr<NG::FrameNode>& node, const std::string& id, bool value);
64 
65     bool PutInt(const RefPtr<NG::FrameNode>& node, const std::string& id, int value);
66 
67     bool PutStringArray(
68         const RefPtr<NG::FrameNode>& node, const std::string& id, const std::vector<std::string>& value);
69 
70     bool PutMultiple(const RefPtr<NG::FrameNode>& node, const std::string& id, const std::string& name, bool value);
71 
72     bool PutMultiple(const RefPtr<NG::FrameNode>& node, const std::string& id, const std::string& name, int index);
73 
74     bool PutMultiple(const RefPtr<NG::FrameNode>& node, const std::string& id, const std::string& name,
75         const std::vector<std::string>& value);
76 
77     void GetNodeData(const std::string& pageUrl, std::unordered_map<std::string, std::string>& nodes);
78 
79     void Clear(const std::string& pageUrl);
80 
81     void GetExposureCfg(const std::string& pageUrl, const std::string& inspectId, ExposureCfg& cfg);
82 
ShouldCollectData()83     bool ShouldCollectData() const
84     {
85         return shouldCollectFull_;
86     }
87 
IsShareNodeEmpty()88     bool IsShareNodeEmpty() const
89     {
90         return mergedConfig_->shareNodes.empty();
91     }
92 
93 private:
94     NodeDataCache();
95     ~NodeDataCache() = default;
96 
97     void Reset();
98 
99     std::shared_ptr<NodeDataContainer> container_;
100 
101     std::shared_ptr<MergedConfig> mergedConfig_;
102 
103     std::shared_mutex configMutex_;
104 
105     std::shared_mutex cacheMutex_;
106 
107     std::string pageUrl_;
108 
109     std::string prePageUrl_;
110 
111     bool shouldCollectFull_ = true;
112 
113     ACE_DISALLOW_COPY_AND_MOVE(NodeDataCache);
114 };
115 } // namespace OHOS::Ace::Recorder
116 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_RECORDER_DATA_CACHE_H
117