1 /* 2 * Copyright (c) 2025 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 OHOS_ABILITY_RUNTIME_EVENT_HANDLER_WRAP_H 17 #define OHOS_ABILITY_RUNTIME_EVENT_HANDLER_WRAP_H 18 19 #include <string> 20 #include <memory> 21 #include <unordered_map> 22 #include <functional> 23 #include <gtest/gtest.h> 24 #include <gmock/gmock.h> 25 #include "ffrt.h" 26 27 namespace OHOS { 28 namespace AAFwk { 29 class TaskHandlerWrap { 30 public: 31 static std::shared_ptr<TaskHandlerWrap> GetFfrtHandler(); 32 MOCK_METHOD(std::shared_ptr<void>, SubmitTaskJust, 33 (std::function<void()> task, const std::string& name, int64_t delay), ()); 34 }; 35 36 class EventTask { 37 public: 38 static bool cancelStatus; Cancel()39 bool Cancel() 40 { 41 return cancelStatus; 42 } 43 }; 44 45 class EventWrap { 46 public: 47 EventWrap() = default; EventWrap(uint32_t eventId)48 explicit EventWrap(uint32_t eventId) : EventWrap(eventId, 0) {} EventWrap(uint32_t eventId,int64_t param)49 EventWrap(uint32_t eventId, int64_t param) {} EventWrap(uint32_t eventId,const std::string & taskName)50 EventWrap(uint32_t eventId, const std::string &taskName) {} 51 52 std::string eventString; GetEventString()53 std::string GetEventString() const 54 { 55 return eventString; 56 } 57 SetTimeout(int64_t)58 void SetTimeout(int64_t) {} SetCreateTime(int64_t)59 void SetCreateTime(int64_t) {} SetEventTask(std::shared_ptr<void> task)60 void SetEventTask(std::shared_ptr<void> task) {} GetEventTask()61 EventTask GetEventTask() const { return EventTask(); } 62 bool sameStatus = false; IsSame(const EventWrap & other)63 bool IsSame(const EventWrap &other) const { return sameStatus; } 64 65 int64_t createTime = 0; GetCreateTime()66 int64_t GetCreateTime() const { return createTime; } 67 int64_t timeout = 0; GetTimeout()68 int64_t GetTimeout() const { return timeout; } 69 }; 70 71 class EventHandlerWrap : public std::enable_shared_from_this<EventHandlerWrap> { 72 public: 73 EventHandlerWrap(); 74 EventHandlerWrap(std::shared_ptr<TaskHandlerWrap> taskHandler); 75 EventHandlerWrap(EventHandlerWrap &) = delete; 76 void operator=(EventHandlerWrap &) = delete; 77 virtual ~EventHandlerWrap(); 78 virtual void ProcessEvent(const EventWrap &event); 79 bool SendEvent(uint32_t eventId); 80 bool SendEvent(uint32_t eventId, int64_t delayMillis); 81 bool SendEvent(EventWrap event); 82 bool SendEvent(EventWrap event, int64_t delayMillis, bool forceInsert = true); 83 bool RemoveEvent(uint32_t eventId, int64_t param = 0); 84 bool RemoveEvent(uint32_t eventId, const std::string &taskName); 85 bool RemoveEvent(EventWrap event, bool force = true); 86 87 protected: 88 std::shared_ptr<TaskHandlerWrap> taskHandler_; 89 std::function<void(const EventWrap&)> eventCallback_; 90 91 std::unique_ptr<ffrt::mutex> eventMutex_; 92 std::unordered_map<std::string, EventWrap> eventMap_; 93 }; 94 } // namespace AAFWK 95 } // namespace OHOS 96 #endif // OHOS_ABILITY_RUNTIME_EVENT_HANDLER_WRAP_H