• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 
24 #include "task_handler_wrap.h"
25 
26 namespace OHOS {
27 namespace AAFwk {
28 class EventDataBase {
29 public:
30     virtual ~EventDataBase() = default;
31 };
32 class EventWrap {
33 public:
EventWrap(uint32_t eventId)34     EventWrap(uint32_t eventId) : EventWrap(eventId, 0) {}
EventWrap(uint32_t eventId,int64_t param)35     EventWrap(uint32_t eventId, int64_t param) : eventId_(eventId), param_(param)
36     {
37         eventData_ = std::make_shared<EventDataBase>();
38     }
EventWrap(uint32_t eventId,int64_t param,bool isExtension)39     EventWrap(uint32_t eventId, int64_t param, bool isExtension) : isExtension_(isExtension), eventId_(eventId),
40         param_(param)
41     {
42         eventData_ = std::make_shared<EventDataBase>();
43     }
EventWrap(uint32_t eventId,int64_t param,bool isExtension,const std::string & taskName)44     EventWrap(uint32_t eventId, int64_t param, bool isExtension,
45         const std::string &taskName) : isExtension_(isExtension), eventId_(eventId), param_(param), taskName_(taskName)
46     {
47         eventData_ = std::make_shared<EventDataBase>();
48     }
EventWrap(uint32_t eventId,const std::string & taskName)49     EventWrap(uint32_t eventId, const std::string &taskName) : eventId_(eventId), taskName_(taskName)
50     {
51         eventData_ = std::make_shared<EventDataBase>();
52     }
EventWrap(uint32_t eventId,std::shared_ptr<EventDataBase> data)53     EventWrap(uint32_t eventId, std::shared_ptr<EventDataBase> data)
54         : eventId_(eventId), param_(0), eventData_(data)
55     {
56         if (!eventData_) {
57             eventData_ = std::make_shared<EventDataBase>();
58         }
59     }
GetEventId()60     uint32_t GetEventId() const
61     {
62         return eventId_;
63     }
GetParam()64     int64_t GetParam() const
65     {
66         return param_;
67     }
GetEventData()68     const std::shared_ptr<EventDataBase>& GetEventData() const
69     {
70         return eventData_;
71     }
GetEventTask()72     const TaskHandle& GetEventTask() const
73     {
74         return eventTask_;
75     }
SetEventTask(const TaskHandle & eventTask)76     void SetEventTask(const TaskHandle &eventTask)
77     {
78         eventTask_ = eventTask;
79     }
GetEventString()80     std::string GetEventString() const
81     {
82         if (taskName_.empty()) {
83             return std::to_string(eventId_) + "_" + std::to_string(param_);
84         }
85         return std::to_string(eventId_) + "_" + taskName_;
86     }
IsSame(const EventWrap & other)87     bool IsSame(const EventWrap &other) const
88     {
89         return eventData_ == other.eventData_;
90     }
SetCreateTime(int64_t createTime)91     void SetCreateTime(int64_t createTime)
92     {
93         createTime_ = createTime;
94     }
GetCreateTime()95     int64_t GetCreateTime() const
96     {
97         return createTime_;
98     }
SetTimeout(int64_t timeout)99     void SetTimeout(int64_t timeout)
100     {
101         timeout_ = timeout;
102     }
GetTimeout()103     int64_t GetTimeout() const
104     {
105         return timeout_;
106     }
IsExtension()107     bool IsExtension() const
108     {
109         return isExtension_;
110     }
111 private:
112     bool isExtension_ = false;
113     uint32_t eventId_ = 0;
114     int64_t timeout_ = 0;
115     int64_t createTime_ = 0;
116     int64_t param_ = 0;
117     std::shared_ptr<EventDataBase> eventData_;
118     TaskHandle eventTask_;
119     std::string taskName_;
120 };
121 
122 class EventHandlerWrap : public std::enable_shared_from_this<EventHandlerWrap> {
123 public:
124     EventHandlerWrap();
125     EventHandlerWrap(std::shared_ptr<TaskHandlerWrap> taskHandler);
126     EventHandlerWrap(EventHandlerWrap &) = delete;
127     void operator=(EventHandlerWrap &) = delete;
128     virtual ~EventHandlerWrap();
129     virtual void ProcessEvent(const EventWrap &event);
130     bool SendEvent(uint32_t eventId);
131     bool SendEvent(uint32_t eventId, int64_t delayMillis);
132     bool SendEvent(EventWrap event);
133     bool SendEvent(EventWrap event, int64_t delayMillis, bool forceInsert = true);
134     bool RemoveEvent(uint32_t eventId, int64_t param = 0);
135     bool RemoveEvent(uint32_t eventId, const std::string &taskName);
136     bool RemoveEvent(EventWrap event, bool force = true);
137 
SetEventCallback(std::function<void (const EventWrap &)> eventCallback)138     void SetEventCallback(std::function<void(const EventWrap&)> eventCallback)
139     {
140         eventCallback_ = eventCallback;
141     }
142 protected:
143     std::shared_ptr<TaskHandlerWrap> taskHandler_;
144     std::function<void(const EventWrap&)> eventCallback_;
145 
146     std::unique_ptr<ffrt::mutex> eventMutex_;
147     std::unordered_map<std::string, EventWrap> eventMap_;
148 };
149 }  // namespace AAFWK
150 }  // namespace OHOS
151 #endif // OHOS_ABILITY_RUNTIME_EVENT_HANDLER_WRAP_H