• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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,std::shared_ptr<EventDataBase> data)39     EventWrap(uint32_t eventId, std::shared_ptr<EventDataBase> data)
40         : eventId_(eventId), param_(0), eventData_(data)
41     {
42         if (!eventData_) {
43             eventData_ = std::make_shared<EventDataBase>();
44         }
45     }
GetEventId()46     uint32_t GetEventId() const
47     {
48         return eventId_;
49     }
GetParam()50     int64_t GetParam() const
51     {
52         return param_;
53     }
GetEventData()54     const std::shared_ptr<EventDataBase>& GetEventData() const
55     {
56         return eventData_;
57     }
GetEventTask()58     const TaskHandle& GetEventTask() const
59     {
60         return eventTask_;
61     }
SetEventTask(const TaskHandle & eventTask)62     void SetEventTask(const TaskHandle &eventTask)
63     {
64         eventTask_ = eventTask;
65     }
GetEventString()66     std::string GetEventString()
67     {
68         return std::to_string(eventId_) + "_" + std::to_string(param_);
69     }
IsSame(const EventWrap & other)70     bool IsSame(const EventWrap &other) const
71     {
72         return eventData_ == other.eventData_;
73     }
SetRunCount(int runCount)74     void SetRunCount(int runCount)
75     {
76         runCount_ = runCount;
77     }
GetRunCount()78     int GetRunCount() const
79     {
80         return runCount_;
81     }
SetTimeout(uint32_t timeout)82     void SetTimeout(uint32_t timeout)
83     {
84         timeout_ = timeout;
85     }
GetTimeout()86     uint32_t GetTimeout() const
87     {
88         return timeout_;
89     }
90 private:
91     uint32_t eventId_;
92     int64_t param_;
93     std::shared_ptr<EventDataBase> eventData_;
94     TaskHandle eventTask_;
95     int runCount_ = 0;
96     uint32_t timeout_ = 0;
97 };
98 
99 class EventHandlerWrap : public std::enable_shared_from_this<EventHandlerWrap> {
100 public:
101     EventHandlerWrap();
102     EventHandlerWrap(std::shared_ptr<TaskHandlerWrap> taskHandler);
103     EventHandlerWrap(EventHandlerWrap &) = delete;
104     void operator=(EventHandlerWrap &) = delete;
105     virtual ~EventHandlerWrap();
106     virtual void ProcessEvent(const EventWrap &event);
107     bool SendEvent(uint32_t eventId);
108     bool SendEvent(uint32_t eventId, int64_t delayMillis);
109     bool SendEvent(EventWrap event);
110     bool SendEvent(EventWrap event, int64_t delayMillis, bool forceInsert = true);
111     bool RemoveEvent(uint32_t eventId, int64_t param = 0);
112     bool RemoveEvent(EventWrap event, bool force = true);
113 
SetEventCallback(std::function<void (const EventWrap &)> eventCallback)114     void SetEventCallback(std::function<void(const EventWrap&)> eventCallback)
115     {
116         eventCallback_ = eventCallback;
117     }
118 protected:
119     std::shared_ptr<TaskHandlerWrap> taskHandler_;
120     std::function<void(const EventWrap&)> eventCallback_;
121 
122     std::unique_ptr<ffrt::mutex> eventMutex_;
123     std::unordered_map<std::string, EventWrap> eventMap_;
124 };
125 }  // namespace AAFWK
126 }  // namespace OHOS
127 #endif // OHOS_ABILITY_RUNTIME_EVENT_HANDLER_WRAP_H