1e0e9324cSopenharmony_ci/*
2e0e9324cSopenharmony_ci * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3e0e9324cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e0e9324cSopenharmony_ci * you may not use this file except in compliance with the License.
5e0e9324cSopenharmony_ci * You may obtain a copy of the License at
6e0e9324cSopenharmony_ci *
7e0e9324cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e0e9324cSopenharmony_ci *
9e0e9324cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e0e9324cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e0e9324cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e0e9324cSopenharmony_ci * See the License for the specific language governing permissions and
13e0e9324cSopenharmony_ci * limitations under the License.
14e0e9324cSopenharmony_ci */
15e0e9324cSopenharmony_ci
16e0e9324cSopenharmony_ci#include "event_base.h"
17e0e9324cSopenharmony_ci#include "common/common_macro.h"
18e0e9324cSopenharmony_ci#include "common/sharing_log.h"
19e0e9324cSopenharmony_ci#include "event_manager.h"
20e0e9324cSopenharmony_ci
21e0e9324cSopenharmony_cinamespace OHOS {
22e0e9324cSopenharmony_cinamespace Sharing {
23e0e9324cSopenharmony_ci
24e0e9324cSopenharmony_ciint32_t EventEmitter::SendEvent(SharingEvent &event)
25e0e9324cSopenharmony_ci{
26e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
27e0e9324cSopenharmony_ci    event.emitterType = classType_;
28e0e9324cSopenharmony_ci    return EventManager::GetInstance().PushEvent(event);
29e0e9324cSopenharmony_ci}
30e0e9324cSopenharmony_ci
31e0e9324cSopenharmony_ciint32_t EventEmitter::SendSyncEvent(SharingEvent &event)
32e0e9324cSopenharmony_ci{
33e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
34e0e9324cSopenharmony_ci    event.emitterType = classType_;
35e0e9324cSopenharmony_ci    return EventManager::GetInstance().PushSyncEvent(event);
36e0e9324cSopenharmony_ci}
37e0e9324cSopenharmony_ci
38e0e9324cSopenharmony_ciint32_t EventListener::Register()
39e0e9324cSopenharmony_ci{
40e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
41e0e9324cSopenharmony_ci    return EventManager::GetInstance().AddListener(shared_from_this());
42e0e9324cSopenharmony_ci}
43e0e9324cSopenharmony_ci
44e0e9324cSopenharmony_ciint32_t EventListener::UnRegister()
45e0e9324cSopenharmony_ci{
46e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
47e0e9324cSopenharmony_ci    return EventManager::GetInstance().DelListener(shared_from_this());
48e0e9324cSopenharmony_ci}
49e0e9324cSopenharmony_ci
50e0e9324cSopenharmony_ciint32_t EventListener::AddAcceptEventType(EventType eventType)
51e0e9324cSopenharmony_ci{
52e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
53e0e9324cSopenharmony_ci    acceptEvents_.emplace_back(eventType);
54e0e9324cSopenharmony_ci    return 0;
55e0e9324cSopenharmony_ci}
56e0e9324cSopenharmony_ci
57e0e9324cSopenharmony_ciint32_t EventListener::DelAcceptEventType(EventType eventType)
58e0e9324cSopenharmony_ci{
59e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
60e0e9324cSopenharmony_ci    for (auto it = acceptEvents_.begin(); it != acceptEvents_.end(); it++) {
61e0e9324cSopenharmony_ci        if (*it == eventType) {
62e0e9324cSopenharmony_ci            acceptEvents_.erase(it);
63e0e9324cSopenharmony_ci            break;
64e0e9324cSopenharmony_ci        }
65e0e9324cSopenharmony_ci    }
66e0e9324cSopenharmony_ci
67e0e9324cSopenharmony_ci    return 0;
68e0e9324cSopenharmony_ci}
69e0e9324cSopenharmony_ci
70e0e9324cSopenharmony_cibool EventListener::IsAcceptType(EventType eventType)
71e0e9324cSopenharmony_ci{
72e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
73e0e9324cSopenharmony_ci    for (auto &type : acceptEvents_) {
74e0e9324cSopenharmony_ci        if (type == eventType) {
75e0e9324cSopenharmony_ci            return true;
76e0e9324cSopenharmony_ci        }
77e0e9324cSopenharmony_ci    }
78e0e9324cSopenharmony_ci
79e0e9324cSopenharmony_ci    return false;
80e0e9324cSopenharmony_ci}
81e0e9324cSopenharmony_ci
82e0e9324cSopenharmony_cistd::list<EventType> EventListener::GetAcceptTypes()
83e0e9324cSopenharmony_ci{
84e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
85e0e9324cSopenharmony_ci    return acceptEvents_;
86e0e9324cSopenharmony_ci}
87e0e9324cSopenharmony_ci
88e0e9324cSopenharmony_civoid EventListener::SetListenerClassType(ClassType classType)
89e0e9324cSopenharmony_ci{
90e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
91e0e9324cSopenharmony_ci    classType_ = classType;
92e0e9324cSopenharmony_ci}
93e0e9324cSopenharmony_ci
94e0e9324cSopenharmony_ciClassType EventListener::GetListenerClassType()
95e0e9324cSopenharmony_ci{
96e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
97e0e9324cSopenharmony_ci    return classType_;
98e0e9324cSopenharmony_ci}
99e0e9324cSopenharmony_ci
100e0e9324cSopenharmony_civoid EventChecker::CheckEvent(EventMsg::Ptr eventMsg)
101e0e9324cSopenharmony_ci{
102e0e9324cSopenharmony_ci    RETURN_IF_NULL(eventMsg);
103e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
104e0e9324cSopenharmony_ci
105e0e9324cSopenharmony_ci    if (EVENT_CONTEXT_BASE < eventMsg->type && eventMsg->type < EVENT_MEDIA_BASE) {
106e0e9324cSopenharmony_ci        if (eventMsg->toMgr != MODULE_CONTEXT) {
107e0e9324cSopenharmony_ci            SHARING_LOGE("set to context type: %{public}d to: %{public}d.", eventMsg->type, eventMsg->toMgr);
108e0e9324cSopenharmony_ci            eventMsg->toMgr = MODULE_CONTEXT;
109e0e9324cSopenharmony_ci        }
110e0e9324cSopenharmony_ci    } else if (EVENT_MEDIA_BASE < eventMsg->type && eventMsg->type < EVENT_INTERACTION_BASE) {
111e0e9324cSopenharmony_ci        if (eventMsg->toMgr != MODULE_MEDIACHANNEL) {
112e0e9324cSopenharmony_ci            SHARING_LOGE("set to mediachannel type: %{public}d to: %{public}d.", eventMsg->type, eventMsg->toMgr);
113e0e9324cSopenharmony_ci            eventMsg->toMgr = MODULE_MEDIACHANNEL;
114e0e9324cSopenharmony_ci        }
115e0e9324cSopenharmony_ci    } else if (EVENT_INTERACTION_BASE < eventMsg->type && eventMsg->type < EVENT_CONFIGURE_BASE) {
116e0e9324cSopenharmony_ci        if (eventMsg->toMgr != MODULE_INTERACTION) {
117e0e9324cSopenharmony_ci            SHARING_LOGE("set to interaction type: %{public}d to: %{public}d.", eventMsg->type, eventMsg->toMgr);
118e0e9324cSopenharmony_ci            eventMsg->toMgr = MODULE_INTERACTION;
119e0e9324cSopenharmony_ci        }
120e0e9324cSopenharmony_ci    }
121e0e9324cSopenharmony_ci}
122e0e9324cSopenharmony_ci
123e0e9324cSopenharmony_ci} // namespace Sharing
124e0e9324cSopenharmony_ci} // namespace OHOS