• 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 #include "sys_dispatcher.h"
17 #include <cstdio>
18 #include <memory>
19 
20 #include "event.h"
21 #include "logger.h"
22 #include "plugin_factory.h"
23 #include "sys_event.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 REGISTER(SysEventDispatcher);
28 DEFINE_LOG_TAG("SysEventDispatcher");
29 
OnLoad()30 void SysEventDispatcher::OnLoad()
31 {
32     auto notifyFunc = [&] (std::shared_ptr<Event> event) -> void {
33         this->GetHiviewContext()->PostUnorderedEvent(shared_from_this(), event);
34     };
35     SysEventServiceAdapter::StartService(this, notifyFunc);
36     SysEventServiceAdapter::SetWorkLoop(this->GetHiviewContext()->GetSharedWorkLoop());
37     HIVIEW_LOGI("OnLoad.");
38 }
39 
OnUnload()40 void SysEventDispatcher::OnUnload()
41 {
42     HIVIEW_LOGI("OnUnload.");
43 }
44 
DispatchEvent(std::shared_ptr<SysEvent> & sysEvent)45 void SysEventDispatcher::DispatchEvent(std::shared_ptr<SysEvent>& sysEvent)
46 {
47     auto dispatchList = GetHiviewContext()->GetDisPatcherInfo(sysEvent->eventType_, sysEvent->eventName_,
48         sysEvent->GetTag(), sysEvent->domain_);
49     for (auto& dispatcher : dispatchList) {
50         auto ptr = dispatcher.lock();
51         if (ptr != nullptr) {
52             ptr->OnEventListeningCallback(*sysEvent);
53         }
54     }
55 }
56 
Convert2SysEvent(std::shared_ptr<Event> & event)57 std::shared_ptr<SysEvent> SysEventDispatcher::Convert2SysEvent(std::shared_ptr<Event>& event)
58 {
59     if (event == nullptr) {
60         HIVIEW_LOGE("event is null");
61         return nullptr;
62     }
63     if (event->messageType_ != Event::MessageType::SYS_EVENT) {
64         HIVIEW_LOGE("receive out of sys event type");
65         return nullptr;
66     }
67     std::shared_ptr<SysEvent> sysEvent = Event::DownCastTo<SysEvent>(event);
68     if (sysEvent == nullptr) {
69         HIVIEW_LOGE("sysevent is null");
70     }
71     return sysEvent;
72 }
73 
OnEvent(std::shared_ptr<Event> & event)74 bool SysEventDispatcher::OnEvent(std::shared_ptr<Event> &event)
75 {
76     auto sysEvent = Convert2SysEvent(event);
77     DispatchEvent(sysEvent);
78     SysEventServiceAdapter::OnSysEvent(sysEvent);
79     return true;
80 }
81 } // namespace HiviewDFX
82 } // namespace OHOS
83