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