1 /*
2  * Copyright (c) 2021 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_EVENT_BACK_END_EVENT_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_BACK_END_EVENT_MANAGER_H
18 
19 #include <functional>
20 #include <list>
21 #include <mutex>
22 #include <unordered_map>
23 
24 #include "base/log/log.h"
25 #include "base/utils/singleton.h"
26 #include "core/event/ace_event_handler.h"
27 
28 namespace OHOS::Ace {
29 
30 // Note that this class is not thread-safe, and is processed on the UI thread of ACE uniformly.
31 // Uses BackEndEventManager to get available id.
32 // This class is only used by BackEndEventManager.
33 class ACE_FORCE_EXPORT BackEndEventIdManager final : public Singleton<BackEndEventIdManager> {
34     DECLARE_SINGLETON(BackEndEventIdManager);
35 
36 public:
37     std::string GetAvailableId();
38 };
39 
40 
41 // Note that this class is not thread-safe, and is processed on the UI thread of ACE uniformly.
42 // Need to use the void return type event, if you need to return the result, use the reference out parameter.
43 template<class>
44 class BackEndEventManager;
45 
46 template<class... Args>
47 class BackEndEventManager<void(Args...)> final : public Singleton<BackEndEventManager<void(Args...)>> {
48     DECLARE_SINGLETON(BackEndEventManager<void(Args...)>);
49 
50 public:
51     // Gets the globally unique event ID of the backend for subsequent event binding.
GetAvailableMarker()52     EventMarker GetAvailableMarker() const
53     {
54         // For back end event, the page id is -1.
55         constexpr int32_t pageId = -1;
56         return EventMarker(BackEndEventIdManager::GetInstance().GetAvailableId(), "", pageId, false);
57     }
58 
BindBackendEvent(const EventMarker & marker,const std::function<void (Args...)> & event)59     void BindBackendEvent(const EventMarker& marker, const std::function<void(Args...)>& event)
60     {
61         bool isSuccess = false;
62         {
63             std::lock_guard<std::mutex> lock(mutex_);
64             auto result = eventMap_.try_emplace(marker.GetData().eventId, event);
65             isSuccess = result.second;
66         }
67         if (!isSuccess) {
68             LOGE("fail to bind back end event due to event id is duplicate!");
69         }
70     }
71 
FireBackEndEvent(const EventMarker & marker,Args &&...args)72     void FireBackEndEvent(const EventMarker& marker, Args&&... args)
73     {
74         std::function<void(Args...)> func;
75         {
76             std::lock_guard<std::mutex> lock(mutex_);
77             auto iter = eventMap_.find(marker.GetData().eventId);
78             if (iter != eventMap_.end()) {
79                 func = iter->second;
80             }
81         }
82         if (!func) {
83             LOGE("fail to trigger back end event due to no such event!");
84             return;
85         }
86         func(std::forward<Args>(args)...);
87     }
88 
89     // When the event does not need to be used, release the corresponding event ID
RemoveBackEndEvent(const EventMarker & marker)90     void RemoveBackEndEvent(const EventMarker& marker)
91     {
92         std::lock_guard<std::mutex> lock(mutex_);
93         eventMap_.erase(marker.GetData().eventId);
94     }
95 
96 private:
97     std::mutex mutex_;
98     std::unordered_map<std::string, std::function<void(Args...)>> eventMap_;
99 };
100 
101 template<class... Args>
102 BackEndEventManager<void(Args...)>::BackEndEventManager() = default;
103 
104 template<class... Args>
105 BackEndEventManager<void(Args...)>::~BackEndEventManager() = default;
106 
107 } // namespace OHOS::Ace
108 
109 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_BACK_END_EVENT_MANAGER_H
110