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