• 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 <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 
30 namespace OHOS::Ace::Recorder {
31 constexpr int32_t MAX_SIZE_PER_PAGE = 50;
32 
33 constexpr int32_t MAX_DATA_LENGTH = 100;
34 
35 const std::string GetCurrentPageUrl();
36 
37 using NodeDataContainer = std::unordered_map<std::string, std::unordered_map<std::string, std::string>>;
38 
39 struct MergedConfig {
40     std::unordered_map<std::string, std::unordered_set<std::string>> shareNodes;
41     std::unordered_map<std::string, std::unordered_set<ExposureCfg, ExposureCfgHash>> exposureNodes;
42 };
43 
44 class NodeDataCache final {
45 public:
46     static NodeDataCache& Get();
47 
48     void OnPageReady();
49 
50     void OnPageShow(const std::string& pageUrl);
51 
52     void OnBeforePagePop(bool destroy = false);
53 
54     void UpdateConfig(std::shared_ptr<MergedConfig>&& mergedConfig);
55 
56     bool PutString(const std::string& id, const std::string& value);
57 
58     bool PutBool(const std::string& id, bool value);
59 
60     bool PutInt(const std::string& id, int value);
61 
62     bool PutStringArray(const std::string& id, const std::vector<std::string>& value);
63 
64     bool PutMultiple(const std::string& id, const std::string& name, bool value);
65 
66     bool PutMultiple(const std::string& id, const std::string& name, int index);
67 
68     bool PutMultiple(const std::string& id, const std::string& name, const std::vector<std::string>& value);
69 
70     void GetNodeData(const std::string& pageUrl, std::unordered_map<std::string, std::string>& nodes);
71 
72     void Clear(const std::string& pageUrl);
73 
74     void GetExposureCfg(const std::string& inspectId, ExposureCfg& cfg);
75 
76 private:
77     NodeDataCache();
78     ~NodeDataCache() = default;
79 
80     void Reset();
81 
82     std::shared_ptr<NodeDataContainer> container_;
83 
84     std::shared_ptr<MergedConfig> mergedConfig_;
85 
86     std::mutex cacheLock_;
87 
88     std::string pageUrl_;
89 
90     std::string prePageUrl_;
91 
92     bool shouldCollectFull_ = true;
93 
94     ACE_DISALLOW_COPY_AND_MOVE(NodeDataCache);
95 };
96 } // namespace OHOS::Ace::Recorder
97 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_RECORDER_DATA_CACHE_H
98