1020a203aSopenharmony_ci/*
2020a203aSopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3020a203aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4020a203aSopenharmony_ci * you may not use this file except in compliance with the License.
5020a203aSopenharmony_ci * You may obtain a copy of the License at
6020a203aSopenharmony_ci *
7020a203aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8020a203aSopenharmony_ci *
9020a203aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10020a203aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11020a203aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12020a203aSopenharmony_ci * See the License for the specific language governing permissions and
13020a203aSopenharmony_ci * limitations under the License.
14020a203aSopenharmony_ci */
15020a203aSopenharmony_ci
16020a203aSopenharmony_ci#include "sys_dispatcher.h"
17020a203aSopenharmony_ci#include <cstdio>
18020a203aSopenharmony_ci#include <memory>
19020a203aSopenharmony_ci
20020a203aSopenharmony_ci#include "event.h"
21020a203aSopenharmony_ci#include "hiview_logger.h"
22020a203aSopenharmony_ci#include "plugin_factory.h"
23020a203aSopenharmony_ci#include "sys_event_service_adapter.h"
24020a203aSopenharmony_ci#include "sys_event.h"
25020a203aSopenharmony_ci
26020a203aSopenharmony_cinamespace OHOS {
27020a203aSopenharmony_cinamespace HiviewDFX {
28020a203aSopenharmony_ciREGISTER(SysEventDispatcher);
29020a203aSopenharmony_ciDEFINE_LOG_TAG("SysEventDispatcher");
30020a203aSopenharmony_ci
31020a203aSopenharmony_civoid SysEventDispatcher::OnLoad()
32020a203aSopenharmony_ci{
33020a203aSopenharmony_ci    HIVIEW_LOGI("OnLoad.");
34020a203aSopenharmony_ci}
35020a203aSopenharmony_ci
36020a203aSopenharmony_civoid SysEventDispatcher::OnUnload()
37020a203aSopenharmony_ci{
38020a203aSopenharmony_ci    HIVIEW_LOGI("OnUnload.");
39020a203aSopenharmony_ci}
40020a203aSopenharmony_ci
41020a203aSopenharmony_civoid SysEventDispatcher::DispatchEvent(std::shared_ptr<SysEvent>& sysEvent)
42020a203aSopenharmony_ci{
43020a203aSopenharmony_ci    auto dispatchList = GetHiviewContext()->GetDisPatcherInfo(sysEvent->eventType_, sysEvent->eventName_,
44020a203aSopenharmony_ci        sysEvent->GetTag(), sysEvent->domain_);
45020a203aSopenharmony_ci    for (auto& dispatcher : dispatchList) {
46020a203aSopenharmony_ci        auto ptr = dispatcher.lock();
47020a203aSopenharmony_ci        if (ptr != nullptr) {
48020a203aSopenharmony_ci            ptr->OnEventListeningCallback(*sysEvent);
49020a203aSopenharmony_ci        }
50020a203aSopenharmony_ci    }
51020a203aSopenharmony_ci}
52020a203aSopenharmony_ci
53020a203aSopenharmony_cistd::shared_ptr<SysEvent> SysEventDispatcher::Convert2SysEvent(std::shared_ptr<Event>& event)
54020a203aSopenharmony_ci{
55020a203aSopenharmony_ci    if (event == nullptr) {
56020a203aSopenharmony_ci        HIVIEW_LOGE("event is null");
57020a203aSopenharmony_ci        return nullptr;
58020a203aSopenharmony_ci    }
59020a203aSopenharmony_ci    if (event->messageType_ != Event::MessageType::SYS_EVENT) {
60020a203aSopenharmony_ci        HIVIEW_LOGE("receive out of sys event type");
61020a203aSopenharmony_ci        return nullptr;
62020a203aSopenharmony_ci    }
63020a203aSopenharmony_ci    std::shared_ptr<SysEvent> sysEvent = Event::DownCastTo<SysEvent>(event);
64020a203aSopenharmony_ci    if (sysEvent == nullptr) {
65020a203aSopenharmony_ci        HIVIEW_LOGE("sysevent is null");
66020a203aSopenharmony_ci    }
67020a203aSopenharmony_ci    return sysEvent;
68020a203aSopenharmony_ci}
69020a203aSopenharmony_ci
70020a203aSopenharmony_cibool SysEventDispatcher::OnEvent(std::shared_ptr<Event> &event)
71020a203aSopenharmony_ci{
72020a203aSopenharmony_ci    auto sysEvent = Convert2SysEvent(event);
73020a203aSopenharmony_ci    if (sysEvent == nullptr) {
74020a203aSopenharmony_ci        return false;
75020a203aSopenharmony_ci    }
76020a203aSopenharmony_ci    DispatchEvent(sysEvent);
77020a203aSopenharmony_ci    SysEventServiceAdapter::OnSysEvent(sysEvent);
78020a203aSopenharmony_ci    return true;
79020a203aSopenharmony_ci}
80020a203aSopenharmony_ci} // namespace HiviewDFX
81020a203aSopenharmony_ci} // namespace OHOS
82