1 /* 2 * Copyright (c) 2021-2022 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_HIVIEWDFX_SYS_EVENT_SERVICE_OHOS_H 17 #define OHOS_HIVIEWDFX_SYS_EVENT_SERVICE_OHOS_H 18 19 #include <atomic> 20 #include <functional> 21 #include <vector> 22 #include <unordered_map> 23 24 #include "data_publisher.h" 25 #include "event.h" 26 #include "event_query_wrapper_builder.h" 27 #include "iquery_base_callback.h" 28 #include "iquery_sys_event_callback.h" 29 #include "isys_event_callback.h" 30 #include "query_argument.h" 31 #include "singleton.h" 32 #include "sys_event_dao.h" 33 #include "sys_event_query.h" 34 #include "sys_event_query_rule.h" 35 #include "sys_event_rule.h" 36 #include "sys_event_service_stub.h" 37 #include "system_ability.h" 38 39 using CallbackObjectOhos = OHOS::sptr<OHOS::IRemoteObject>; 40 using SysEventCallbackPtrOhos = OHOS::sptr<OHOS::HiviewDFX::ISysEventCallback>; 41 using SysEventRuleGroupOhos = std::vector<OHOS::HiviewDFX::SysEventRule>; 42 using SysEventQueryRuleGroupOhos = std::vector<OHOS::HiviewDFX::SysEventQueryRule>; 43 using RegisteredListeners = std::map<CallbackObjectOhos, std::pair<int32_t, SysEventRuleGroupOhos>>; 44 45 namespace OHOS { 46 namespace HiviewDFX { 47 using NotifySysEvent = std::function<void (std::shared_ptr<Event>)>; 48 using GetTagByDomainNameFunc = std::function<std::string(std::string, std::string)>; 49 using GetTypeByDomainNameFunc = std::function<int(std::string, std::string)>; 50 51 class SysEventServiceBase { 52 }; 53 54 class CallbackDeathRecipient : public IRemoteObject::DeathRecipient { 55 public: 56 CallbackDeathRecipient() = default; 57 virtual ~CallbackDeathRecipient() = default; 58 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 59 }; 60 61 class SysEventServiceOhos : public SystemAbility, 62 public SysEventServiceStub { 63 DECLARE_SYSTEM_ABILITY(SysEventServiceOhos); 64 public: 65 DISALLOW_COPY_AND_MOVE(SysEventServiceOhos); SysEventServiceOhos()66 SysEventServiceOhos() 67 : deathRecipient_(new CallbackDeathRecipient()), isDebugMode_(false), dataPublisher_(new DataPublisher()){}; 68 virtual ~SysEventServiceOhos() = default; 69 70 static sptr<SysEventServiceOhos> GetInstance(); 71 static void StartService(SysEventServiceBase* service, 72 const OHOS::HiviewDFX::NotifySysEvent notify); 73 static SysEventServiceBase* GetSysEventService( 74 OHOS::HiviewDFX::SysEventServiceBase* service = nullptr); 75 void OnSysEvent(std::shared_ptr<OHOS::HiviewDFX::SysEvent>& sysEvent); 76 void UpdateEventSeq(int64_t seq); 77 int32_t AddListener(const SysEventRuleGroupOhos& rules, const SysEventCallbackPtrOhos& callback) override; 78 int32_t RemoveListener(const SysEventCallbackPtrOhos& callback) override; 79 int32_t Query(const QueryArgument& queryArgument, const SysEventQueryRuleGroupOhos& rules, 80 const OHOS::sptr<OHOS::HiviewDFX::IQuerySysEventCallback>& callback) override; 81 int32_t SetDebugMode(const SysEventCallbackPtrOhos& callback, bool mode) override; 82 void OnRemoteDied(const wptr<IRemoteObject> &remote); 83 void BindGetTagFunc(const GetTagByDomainNameFunc& getTagFunc); 84 void BindGetTypeFunc(const GetTypeByDomainNameFunc& getTypeFunc); 85 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 86 int64_t AddSubscriber(const SysEventQueryRuleGroupOhos &rules) override; 87 int32_t RemoveSubscriber() override; 88 int64_t Export(const QueryArgument &queryArgument, const SysEventQueryRuleGroupOhos &rules) override; 89 void SetWorkLoop(std::shared_ptr<EventLoop> looper); 90 91 private: 92 bool HasAccessPermission() const; 93 bool BuildEventQuery(std::shared_ptr<EventQueryWrapperBuilder> builder, const SysEventQueryRuleGroupOhos& rules); 94 std::string GetTagByDomainAndName(const std::string& eventDomain, const std::string& eventName); 95 uint32_t GetTypeByDomainAndName(const std::string& eventDomain, const std::string& eventName); 96 void MergeEventList(const std::vector<SysEventQueryRule>& rules, std::vector<std::string>& events) const; 97 98 private: 99 std::mutex mutex_; 100 sptr<CallbackDeathRecipient> deathRecipient_; 101 RegisteredListeners registeredListeners_; 102 bool isDebugMode_; 103 SysEventCallbackPtrOhos debugModeCallback_; 104 GetTagByDomainNameFunc getTagFunc_; 105 GetTypeByDomainNameFunc getTypeFunc_; 106 static OHOS::HiviewDFX::NotifySysEvent gISysEventNotify_; 107 std::atomic<int64_t> curSeq = 0; 108 std::shared_ptr<DataPublisher> dataPublisher_; 109 110 private: 111 static sptr<SysEventServiceOhos> instance; 112 }; 113 } // namespace HiviewDFX 114 } // namespace OHOS 115 116 #endif // OHOS_HIVIEWDFX_SYS_EVENT_SERVICE_OHOS_H