• 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_ACE_EVENT_HANDLER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_ACE_EVENT_HANDLER_H
18 
19 #include <memory>
20 
21 #include "base/memory/ace_type.h"
22 #include "core/common/container_scope.h"
23 #include "core/event/ace_events.h"
24 #include "core/event/key_event.h"
25 #include "core/event/rotation_event.h"
26 
27 namespace OHOS::Ace {
28 
29 class GestureEvent;
30 
31 class EventMarker final {
32 public:
33     using Function = std::function<void()>;
34     using StrFunction = std::function<void(const std::string& info)>;
35     using ArgFunction = std::function<void(BaseEventInfo* info)>;
36 
37     struct Data final {
Datafinal38         Data() : instanceId(ContainerScope::CurrentId()) {}
39         explicit Data(const Data& other) = default;
40         explicit Data(Data&& other) = default;
Datafinal41         explicit Data(Function&& preFunc) : preFunction(std::move(preFunc)), instanceId(ContainerScope::CurrentId()) {}
Datafinal42         explicit Data(StrFunction&& func) : uiStrFunction(std::move(func)), instanceId(ContainerScope::CurrentId()) {}
Datafinal43         Data(const std::string& eventId, const std::string& eventType, int32_t pageId, bool isFront)
44             : eventId(eventId), eventType(eventType), pageId(pageId), isFront(isFront),
45               instanceId(ContainerScope::CurrentId())
46         {}
Datafinal47         Data(Function&& func, const std::string& eventType, int32_t pageId, bool isFront)
48             : uiFunction(std::move(func)), eventId("-1"), eventType(eventType), pageId(pageId), isFront(isFront),
49               isDeclarativeUi(true), instanceId(ContainerScope::CurrentId())
50         {}
Datafinal51         Data(ArgFunction&& func, const std::string& eventType, int32_t pageId, bool isFront)
52             : uiArgFunction(std::move(func)), eventId("-1"), eventType(eventType), pageId(pageId), isFront(isFront),
53               isDeclarativeUi(true), instanceId(ContainerScope::CurrentId())
54         {}
Datafinal55         Data(StrFunction&& func, const std::string& eventType, int32_t pageId, bool isFront)
56             : uiStrFunction(std::move(func)), eventId("-1"), eventType(eventType), pageId(pageId), isFront(isFront),
57               isDeclarativeUi(true), instanceId(ContainerScope::CurrentId())
58         {}
59 
60         ~Data() = default;
61 
GetEventParamfinal62         std::string GetEventParam() const
63         {
64             return std::string("\"").append(eventType).append("\",");
65         }
66 
67         Function preFunction;
68         Function uiFunction;
69         ArgFunction uiArgFunction;
70         StrFunction uiStrFunction;
71         std::string eventId;
72         std::string eventType;
73         int32_t pageId = -1;
74         bool isFront = true;
75         bool isDeclarativeUi = false;
76         bool isCatchMode = true;
77         int32_t instanceId = -1;
78     };
79 
80     EventMarker() = default;
81     explicit EventMarker(const std::string& eventId, const std::string& eventType = std::string(), int32_t pageId = -1,
82         bool isFront = true)
data_(std::make_unique<Data> (eventId,eventType,pageId,isFront))83         : data_(std::make_unique<Data>(eventId, eventType, pageId, isFront))
84     {}
85 
86     EventMarker(EventMarker&& other) = default;
87     explicit EventMarker(
88         Function&& func, const std::string& eventType = std::string(), int32_t pageId = -1, bool isFront = true)
data_(std::make_unique<Data> (std::move (func),eventType,pageId,isFront))89         : data_(std::make_unique<Data>(std::move(func), eventType, pageId, isFront))
90     {}
91 
92     explicit EventMarker(
93         ArgFunction&& func, const std::string& eventType = std::string(), int32_t pageId = -1, bool isFront = true)
data_(std::make_unique<Data> (std::move (func),eventType,pageId,isFront))94         : data_(std::make_unique<Data>(std::move(func), eventType, pageId, isFront))
95     {}
96     explicit EventMarker(
97         StrFunction&& func, const std::string& eventType = std::string(), int32_t pageId = -1, bool isFront = true)
data_(std::make_unique<Data> (std::move (func),eventType,pageId,isFront))98         : data_(std::make_unique<Data>(std::move(func), eventType, pageId, isFront))
99     {}
100 
EventMarker(const EventMarker & other)101     EventMarker(const EventMarker& other)
102     {
103         if (other.data_) {
104             ContainerScope id(other.data_->instanceId);
105             data_ = std::make_unique<Data>(*other.data_);
106         }
107     }
108     ~EventMarker() = default;
109 
110     EventMarker& operator=(EventMarker&& other) = default;
111     EventMarker& operator=(const EventMarker& other)
112     {
113         data_ = other.data_ ? std::make_unique<Data>(*other.data_) : nullptr;
114         return *this;
115     }
116 
Reset()117     void Reset()
118     {
119         data_.release();
120     }
121 
122     bool operator!=(const std::string& markerEventId) const
123     {
124         return GetData().eventId != markerEventId;
125     }
126 
127     bool operator==(const std::string& markerEventId) const
128     {
129         return GetData().eventId == markerEventId;
130     }
131 
IsEmpty()132     bool IsEmpty() const
133     {
134         return !data_ || data_->eventId.empty();
135     }
136 
SetPreFunction(Function && preFunc)137     void SetPreFunction(Function&& preFunc)
138     {
139         if (data_) {
140             data_->preFunction = std::move(preFunc);
141         } else {
142             data_ = std::make_unique<Data>(std::move(preFunc));
143         }
144     }
145 
SetUiStrFunction(StrFunction && uiStrFunction)146     void SetUiStrFunction(StrFunction&& uiStrFunction)
147     {
148         if (data_) {
149             data_->uiStrFunction = std::move(uiStrFunction);
150         } else {
151             data_ = std::make_unique<Data>(std::move(uiStrFunction));
152         }
153     }
154 
GetUiStrFunction()155     StrFunction GetUiStrFunction() const
156     {
157         if (data_) {
158             return data_->uiStrFunction;
159         }
160         return nullptr;
161     }
162 
SetCatchMode(bool isCatchMode)163     void SetCatchMode(bool isCatchMode)
164     {
165         if (data_) {
166             data_->isCatchMode = isCatchMode;
167         }
168     }
169 
GetCatchMode()170     bool GetCatchMode() const
171     {
172         return !data_ || data_->isCatchMode;
173     }
174 
CallPreFunction()175     void CallPreFunction() const
176     {
177         if (data_ && data_->preFunction) {
178             data_->preFunction();
179         }
180     }
181 
HasPreFunction()182     bool HasPreFunction() const
183     {
184         return data_ && data_->preFunction;
185     }
186 
CallUiFunction()187     void CallUiFunction() const
188     {
189         if (data_ && data_->uiFunction) {
190             data_->uiFunction();
191         }
192     }
193 
CallUiArgFunction(BaseEventInfo * info)194     void CallUiArgFunction(BaseEventInfo* info) const
195     {
196         if (data_ && data_->uiArgFunction) {
197             data_->uiArgFunction(info);
198         }
199     }
200 
CallUiStrFunction(const std::string & info)201     void CallUiStrFunction(const std::string& info) const
202     {
203         if (data_ && data_->uiStrFunction) {
204             data_->uiStrFunction(info);
205         }
206     }
207 
HasUiStrFunction()208     bool HasUiStrFunction() const
209     {
210         return data_ && (data_->uiStrFunction);
211     }
212 
GetData()213     const Data& GetData() const
214     {
215         if (data_) {
216             return *data_;
217         }
218         static const Data emptyData;
219         return emptyData;
220     }
221 
222 private:
223     std::unique_ptr<Data> data_;
224 };
225 
226 class AceEventHandler : public AceType {
227     DECLARE_ACE_TYPE(AceEventHandler, AceType);
228 
229 public:
230     virtual void HandleAsyncEvent(const EventMarker& eventMarker) = 0;
231     virtual void HandleAsyncEvent(const EventMarker& eventMarker, int32_t param) = 0;
232     virtual void HandleAsyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info) = 0;
233     virtual void HandleAsyncEvent(const EventMarker& eventMarker, const KeyEvent& info) = 0;
HandleAsyncEvent(const EventMarker & eventMarker,const GestureEvent & info)234     virtual void HandleAsyncEvent(const EventMarker& eventMarker, const GestureEvent& info) {};
HandleAsyncEvent(const EventMarker & eventMarker,const RotationEvent & info)235     virtual void HandleAsyncEvent(const EventMarker& eventMarker, const RotationEvent& info) {};
236     // For json dsl event which has json format param.
237     virtual void HandleAsyncEvent(const EventMarker& eventMarker, const std::string& param) = 0;
HandleAsyncEvent(const EventMarker & eventMarker,const std::shared_ptr<BaseEventInfo> & info)238     virtual void HandleAsyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info)
239     {
240         HandleAsyncEvent(eventMarker, *info);
241     }
242 
243     virtual void HandleSyncEvent(const EventMarker& eventMarker, bool& result) = 0;
244     virtual void HandleSyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info, bool& result) = 0;
245     virtual void HandleSyncEvent(const EventMarker& eventMarker, const KeyEvent& info, bool& result) = 0;
246     // For json dsl event which has json format param and json format result.
247     virtual void HandleSyncEvent(const EventMarker& eventMarker, const std::string& param, std::string& result) = 0;
HandleSyncEvent(const EventMarker & eventMarker,const std::shared_ptr<BaseEventInfo> & info)248     virtual void HandleSyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info)
249     {
250         bool result = true;
251         HandleSyncEvent(eventMarker, *info, result);
252     }
253     virtual void HandleSyncEvent(
254         const EventMarker& eventMarker, const std::string& componentId, const int32_t nodeId) = 0;
255 };
256 
257 } // namespace OHOS::Ace
258 
259 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_ACE_EVENT_HANDLER_H
260