• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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         LOGD("the bind event id is %{public}s, the manager is %{private}p", marker.GetData().eventId.c_str(), this);
62 
63         bool isSuccess = false;
64         {
65             std::lock_guard<std::mutex> lock(mutex_);
66             auto result = eventMap_.try_emplace(marker.GetData().eventId, event);
67             isSuccess = result.second;
68         }
69         if (!isSuccess) {
70             LOGE("fail to bind back end event due to event id is duplicate!");
71         }
72     }
73 
FireBackEndEvent(const EventMarker & marker,Args &&...args)74     void FireBackEndEvent(const EventMarker& marker, Args&&... args)
75     {
76         LOGD("the Fire event id is %{public}s, the manager is %{private}p", marker.GetData().eventId.c_str(), this);
77         std::function<void(Args...)> func;
78         {
79             std::lock_guard<std::mutex> lock(mutex_);
80             auto iter = eventMap_.find(marker.GetData().eventId);
81             if (iter != eventMap_.end()) {
82                 func = iter->second;
83             }
84         }
85         if (!func) {
86             LOGE("fail to trigger back end event due to no such event!");
87             return;
88         }
89         func(std::forward<Args>(args)...);
90     }
91 
92     // When the event does not need to be used, release the corresponding event ID
RemoveBackEndEvent(const EventMarker & marker)93     void RemoveBackEndEvent(const EventMarker& marker)
94     {
95         std::lock_guard<std::mutex> lock(mutex_);
96         eventMap_.erase(marker.GetData().eventId);
97     }
98 
99 private:
100     std::mutex mutex_;
101     std::unordered_map<std::string, std::function<void(Args...)>> eventMap_;
102 };
103 
104 template<class... Args>
105 BackEndEventManager<void(Args...)>::BackEndEventManager() = default;
106 
107 template<class... Args>
108 BackEndEventManager<void(Args...)>::~BackEndEventManager() = default;
109 
110 } // namespace OHOS::Ace
111 
112 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_BACK_END_EVENT_MANAGER_H
113