1 /* 2 * Copyright (c) 2021-2022 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) 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) 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) 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) 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 117 bool operator!=(const std::string& markerEventId) const 118 { 119 return GetData().eventId != markerEventId; 120 } 121 122 bool operator==(const std::string& markerEventId) const 123 { 124 return GetData().eventId == markerEventId; 125 } 126 IsEmpty()127 bool IsEmpty() const 128 { 129 return !data_ || data_->eventId.empty(); 130 } 131 SetPreFunction(Function && preFunc)132 void SetPreFunction(Function&& preFunc) 133 { 134 if (data_) { 135 data_->preFunction = std::move(preFunc); 136 } else { 137 data_ = std::make_unique<Data>(std::move(preFunc)); 138 } 139 } 140 SetUiStrFunction(StrFunction && uiStrFunction)141 void SetUiStrFunction(StrFunction&& uiStrFunction) 142 { 143 if (data_) { 144 data_->uiStrFunction = std::move(uiStrFunction); 145 } else { 146 data_ = std::make_unique<Data>(std::move(uiStrFunction)); 147 } 148 } 149 GetUiStrFunction()150 StrFunction GetUiStrFunction() const 151 { 152 if (data_) { 153 return data_->uiStrFunction; 154 } 155 return nullptr; 156 } 157 SetCatchMode(bool isCatchMode)158 void SetCatchMode(bool isCatchMode) 159 { 160 if (data_) { 161 data_->isCatchMode = isCatchMode; 162 } 163 } 164 GetCatchMode()165 bool GetCatchMode() const 166 { 167 return !data_ || data_->isCatchMode; 168 } 169 CallPreFunction()170 void CallPreFunction() const 171 { 172 if (data_ && data_->preFunction) { 173 data_->preFunction(); 174 } 175 } 176 HasPreFunction()177 bool HasPreFunction() const 178 { 179 return data_ && data_->preFunction; 180 } 181 CallUiFunction()182 void CallUiFunction() const 183 { 184 if (data_ && data_->uiFunction) { 185 data_->uiFunction(); 186 } 187 } 188 CallUiArgFunction(BaseEventInfo * info)189 void CallUiArgFunction(BaseEventInfo* info) const 190 { 191 if (data_ && data_->uiArgFunction) { 192 data_->uiArgFunction(info); 193 } 194 } 195 CallUiStrFunction(const std::string & info)196 void CallUiStrFunction(const std::string& info) const 197 { 198 if (data_ && data_->uiStrFunction) { 199 data_->uiStrFunction(info); 200 } 201 } 202 HasUiStrFunction()203 bool HasUiStrFunction() const 204 { 205 return data_ && (data_->uiStrFunction); 206 } 207 GetData()208 const Data& GetData() const 209 { 210 if (data_) { 211 return *data_; 212 } 213 static const Data emptyData; 214 return emptyData; 215 } 216 217 private: 218 std::unique_ptr<Data> data_; 219 }; 220 221 class AceEventHandler : public AceType { 222 DECLARE_ACE_TYPE(AceEventHandler, AceType); 223 224 public: 225 virtual void HandleAsyncEvent(const EventMarker& eventMarker) = 0; 226 virtual void HandleAsyncEvent(const EventMarker& eventMarker, int32_t param) = 0; 227 virtual void HandleAsyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info) = 0; 228 virtual void HandleAsyncEvent(const EventMarker& eventMarker, const KeyEvent& info) = 0; HandleAsyncEvent(const EventMarker & eventMarker,const GestureEvent & info)229 virtual void HandleAsyncEvent(const EventMarker& eventMarker, const GestureEvent& info) {} HandleAsyncEvent(const EventMarker & eventMarker,const RotationEvent & info)230 virtual void HandleAsyncEvent(const EventMarker& eventMarker, const RotationEvent& info) {} 231 // For json dsl event which has json format param. 232 virtual void HandleAsyncEvent(const EventMarker& eventMarker, const std::string& param) = 0; HandleAsyncEvent(const EventMarker & eventMarker,const std::shared_ptr<BaseEventInfo> & info)233 virtual void HandleAsyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info) 234 { 235 HandleAsyncEvent(eventMarker, *info); 236 } 237 238 virtual void HandleSyncEvent(const EventMarker& eventMarker, bool& result) = 0; 239 virtual void HandleSyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info, bool& result) = 0; 240 virtual void HandleSyncEvent(const EventMarker& eventMarker, const KeyEvent& info, bool& result) = 0; 241 // For json dsl event which has json format param and json format result. 242 virtual void HandleSyncEvent(const EventMarker& eventMarker, const std::string& param, std::string& result) = 0; HandleSyncEvent(const EventMarker & eventMarker,const std::shared_ptr<BaseEventInfo> & info)243 virtual void HandleSyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info) 244 { 245 bool result = true; 246 HandleSyncEvent(eventMarker, *info, result); 247 } 248 virtual void HandleSyncEvent(const EventMarker& eventMarker, const std::string& componentId, const int32_t nodeId, 249 const bool isDestroy = false) = 0; 250 }; 251 252 } // namespace OHOS::Ace 253 254 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_ACE_EVENT_HANDLER_H 255