• 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_EVENT_CONTROLLER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_RECORDER_EVENT_CONTROLLER_H
18 
19 #include <shared_mutex>
20 #include <string>
21 #include <unordered_map>
22 #include <vector>
23 
24 #include "interfaces/inner_api/ace/ui_event_observer.h"
25 #include "base/utils/noncopyable.h"
26 #include "core/common/recorder/event_config.h"
27 #include "core/common/recorder/event_definition.h"
28 
29 namespace OHOS::Ace::Recorder {
30 struct UIEventClient {
31     std::shared_ptr<UIEventObserver> observer;
32     EventConfig config;
33 };
34 
35 class EventController final {
36 public:
37     static EventController& Get();
38 
39     void Register(const std::string& config, const std::shared_ptr<UIEventObserver>& observer);
40     void Unregister(const std::shared_ptr<UIEventObserver>& observer);
41 
42     void NotifyEvent(EventCategory category, int32_t eventType,
43         const std::shared_ptr<std::unordered_map<std::string, std::string>>& eventParams);
44 
45     void NotifyEventSync(EventCategory category, int32_t eventType,
46         const std::shared_ptr<std::unordered_map<std::string, std::string>>& eventParams);
47 
48     std::vector<std::string> GetWebJsCodeList();
49 
HasCached()50     bool HasCached() const
51     {
52         return hasCached_;
53     }
54 
HasWebProcessed()55     bool HasWebProcessed() const
56     {
57         return hasWebProcessed_;
58     }
59 
GetCacheJsCode()60     std::string GetCacheJsCode() const
61     {
62         return cacheJsCode_;
63     }
64 
65 private:
66     struct CacheEvent {
67         EventCategory category;
68         int32_t eventType;
69         std::shared_ptr<std::unordered_map<std::string, std::string>> eventParams;
70     };
71     EventController() = default;
72     ~EventController() = default;
73     void NotifyConfigChange();
74     void ApplyNewestConfig(bool isExposureChanged, bool isWebChanged) const;
75     void ApplyExposureCfgInner(const std::shared_ptr<Config>& config, bool isExposureChanged, bool isWebChanged) const;
76     void CacheEventIfNeed(EventCategory category, int32_t eventType,
77         const std::shared_ptr<std::unordered_map<std::string, std::string>>& eventParams);
78     void NotifyCacheEventsIfNeed() const;
79 
80     std::shared_mutex mutable cacheLock_;
81     std::vector<UIEventClient> clientList_;
82     std::vector<CacheEvent> cacheEvents_;
83     bool hasCached_ = false;
84     bool hasWebProcessed_ = false;
85     std::string cacheJsCode_;
86 
87     ACE_DISALLOW_COPY_AND_MOVE(EventController);
88 };
89 } // namespace OHOS::Ace::Recorder
90 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_RECORDER_EVENT_CONTROLLER_H
91