• 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 
55 private:
56     struct CacheEvent {
57         EventCategory category;
58         int32_t eventType;
59         std::shared_ptr<std::unordered_map<std::string, std::string>> eventParams;
60     };
61     EventController() = default;
62     ~EventController() = default;
63     void NotifyConfigChange();
64     void ApplyNewestConfig(bool isExposureChanged) const;
65     void ApplyExposureCfgInner(const std::shared_ptr<Config>& config, bool isExposureChanged) const;
66     void CacheEventIfNeed(EventCategory category, int32_t eventType,
67         const std::shared_ptr<std::unordered_map<std::string, std::string>>& eventParams);
68     void NotifyCacheEventsIfNeed(const UIEventClient& client) const;
69 
70     std::shared_mutex mutable cacheLock_;
71     std::vector<UIEventClient> clientList_;
72     std::shared_mutex mutable cacheEventLock_;
73     std::vector<CacheEvent> cacheEvents_;
74     bool hasCached_ = false;
75 
76     ACE_DISALLOW_COPY_AND_MOVE(EventController);
77 };
78 } // namespace OHOS::Ace::Recorder
79 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_RECORDER_EVENT_CONTROLLER_H
80