1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#ifndef OHOS_ABILITY_RUNTIME_EVENT_HANDLER_WRAP_H
17eace7efcSopenharmony_ci#define OHOS_ABILITY_RUNTIME_EVENT_HANDLER_WRAP_H
18eace7efcSopenharmony_ci
19eace7efcSopenharmony_ci#include <string>
20eace7efcSopenharmony_ci#include <memory>
21eace7efcSopenharmony_ci#include <unordered_map>
22eace7efcSopenharmony_ci#include <functional>
23eace7efcSopenharmony_ci
24eace7efcSopenharmony_ci#include "task_handler_wrap.h"
25eace7efcSopenharmony_ci
26eace7efcSopenharmony_cinamespace OHOS {
27eace7efcSopenharmony_cinamespace AAFwk {
28eace7efcSopenharmony_ciclass EventDataBase {
29eace7efcSopenharmony_cipublic:
30eace7efcSopenharmony_ci    virtual ~EventDataBase() = default;
31eace7efcSopenharmony_ci};
32eace7efcSopenharmony_ciclass EventWrap {
33eace7efcSopenharmony_cipublic:
34eace7efcSopenharmony_ci    EventWrap(uint32_t eventId) : EventWrap(eventId, 0) {}
35eace7efcSopenharmony_ci    EventWrap(uint32_t eventId, int64_t param) : eventId_(eventId), param_(param)
36eace7efcSopenharmony_ci    {
37eace7efcSopenharmony_ci        eventData_ = std::make_shared<EventDataBase>();
38eace7efcSopenharmony_ci    }
39eace7efcSopenharmony_ci    EventWrap(uint32_t eventId, int64_t param, bool isExtension) : eventId_(eventId), param_(param),
40eace7efcSopenharmony_ci        isExtension_(isExtension)
41eace7efcSopenharmony_ci    {
42eace7efcSopenharmony_ci        eventData_ = std::make_shared<EventDataBase>();
43eace7efcSopenharmony_ci    }
44eace7efcSopenharmony_ci    EventWrap(uint32_t eventId, std::shared_ptr<EventDataBase> data)
45eace7efcSopenharmony_ci        : eventId_(eventId), param_(0), eventData_(data)
46eace7efcSopenharmony_ci    {
47eace7efcSopenharmony_ci        if (!eventData_) {
48eace7efcSopenharmony_ci            eventData_ = std::make_shared<EventDataBase>();
49eace7efcSopenharmony_ci        }
50eace7efcSopenharmony_ci    }
51eace7efcSopenharmony_ci    uint32_t GetEventId() const
52eace7efcSopenharmony_ci    {
53eace7efcSopenharmony_ci        return eventId_;
54eace7efcSopenharmony_ci    }
55eace7efcSopenharmony_ci    int64_t GetParam() const
56eace7efcSopenharmony_ci    {
57eace7efcSopenharmony_ci        return param_;
58eace7efcSopenharmony_ci    }
59eace7efcSopenharmony_ci    const std::shared_ptr<EventDataBase>& GetEventData() const
60eace7efcSopenharmony_ci    {
61eace7efcSopenharmony_ci        return eventData_;
62eace7efcSopenharmony_ci    }
63eace7efcSopenharmony_ci    const TaskHandle& GetEventTask() const
64eace7efcSopenharmony_ci    {
65eace7efcSopenharmony_ci        return eventTask_;
66eace7efcSopenharmony_ci    }
67eace7efcSopenharmony_ci    void SetEventTask(const TaskHandle &eventTask)
68eace7efcSopenharmony_ci    {
69eace7efcSopenharmony_ci        eventTask_ = eventTask;
70eace7efcSopenharmony_ci    }
71eace7efcSopenharmony_ci    std::string GetEventString()
72eace7efcSopenharmony_ci    {
73eace7efcSopenharmony_ci        return std::to_string(eventId_) + "_" + std::to_string(param_);
74eace7efcSopenharmony_ci    }
75eace7efcSopenharmony_ci    bool IsSame(const EventWrap &other) const
76eace7efcSopenharmony_ci    {
77eace7efcSopenharmony_ci        return eventData_ == other.eventData_;
78eace7efcSopenharmony_ci    }
79eace7efcSopenharmony_ci    void SetRunCount(int runCount)
80eace7efcSopenharmony_ci    {
81eace7efcSopenharmony_ci        runCount_ = runCount;
82eace7efcSopenharmony_ci    }
83eace7efcSopenharmony_ci    int GetRunCount() const
84eace7efcSopenharmony_ci    {
85eace7efcSopenharmony_ci        return runCount_;
86eace7efcSopenharmony_ci    }
87eace7efcSopenharmony_ci    void SetTimeout(uint32_t timeout)
88eace7efcSopenharmony_ci    {
89eace7efcSopenharmony_ci        timeout_ = timeout;
90eace7efcSopenharmony_ci    }
91eace7efcSopenharmony_ci    uint32_t GetTimeout() const
92eace7efcSopenharmony_ci    {
93eace7efcSopenharmony_ci        return timeout_;
94eace7efcSopenharmony_ci    }
95eace7efcSopenharmony_ci    bool IsExtension() const
96eace7efcSopenharmony_ci    {
97eace7efcSopenharmony_ci        return isExtension_;
98eace7efcSopenharmony_ci    }
99eace7efcSopenharmony_ciprivate:
100eace7efcSopenharmony_ci    uint32_t eventId_;
101eace7efcSopenharmony_ci    int64_t param_;
102eace7efcSopenharmony_ci    std::shared_ptr<EventDataBase> eventData_;
103eace7efcSopenharmony_ci    TaskHandle eventTask_;
104eace7efcSopenharmony_ci    int runCount_ = 0;
105eace7efcSopenharmony_ci    uint32_t timeout_ = 0;
106eace7efcSopenharmony_ci    bool isExtension_ = false;
107eace7efcSopenharmony_ci};
108eace7efcSopenharmony_ci
109eace7efcSopenharmony_ciclass EventHandlerWrap : public std::enable_shared_from_this<EventHandlerWrap> {
110eace7efcSopenharmony_cipublic:
111eace7efcSopenharmony_ci    EventHandlerWrap();
112eace7efcSopenharmony_ci    EventHandlerWrap(std::shared_ptr<TaskHandlerWrap> taskHandler);
113eace7efcSopenharmony_ci    EventHandlerWrap(EventHandlerWrap &) = delete;
114eace7efcSopenharmony_ci    void operator=(EventHandlerWrap &) = delete;
115eace7efcSopenharmony_ci    virtual ~EventHandlerWrap();
116eace7efcSopenharmony_ci    virtual void ProcessEvent(const EventWrap &event);
117eace7efcSopenharmony_ci    bool SendEvent(uint32_t eventId);
118eace7efcSopenharmony_ci    bool SendEvent(uint32_t eventId, int64_t delayMillis);
119eace7efcSopenharmony_ci    bool SendEvent(EventWrap event);
120eace7efcSopenharmony_ci    bool SendEvent(EventWrap event, int64_t delayMillis, bool forceInsert = true);
121eace7efcSopenharmony_ci    bool RemoveEvent(uint32_t eventId, int64_t param = 0);
122eace7efcSopenharmony_ci    bool RemoveEvent(EventWrap event, bool force = true);
123eace7efcSopenharmony_ci
124eace7efcSopenharmony_ci    void SetEventCallback(std::function<void(const EventWrap&)> eventCallback)
125eace7efcSopenharmony_ci    {
126eace7efcSopenharmony_ci        eventCallback_ = eventCallback;
127eace7efcSopenharmony_ci    }
128eace7efcSopenharmony_ciprotected:
129eace7efcSopenharmony_ci    std::shared_ptr<TaskHandlerWrap> taskHandler_;
130eace7efcSopenharmony_ci    std::function<void(const EventWrap&)> eventCallback_;
131eace7efcSopenharmony_ci
132eace7efcSopenharmony_ci    std::unique_ptr<ffrt::mutex> eventMutex_;
133eace7efcSopenharmony_ci    std::unordered_map<std::string, EventWrap> eventMap_;
134eace7efcSopenharmony_ci};
135eace7efcSopenharmony_ci}  // namespace AAFWK
136eace7efcSopenharmony_ci}  // namespace OHOS
137eace7efcSopenharmony_ci#endif // OHOS_ABILITY_RUNTIME_EVENT_HANDLER_WRAP_H