• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 <vector>
21 #include <unordered_map>
22 
23 #include "data_publisher.h"
24 #include "event_query_wrapper_builder.h"
25 #include "iquery_base_callback.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 #include "type/base_types.h"
37 
38 namespace OHOS {
39 namespace HiviewDFX {
40 class CallbackDeathRecipient : public IRemoteObject::DeathRecipient {
41 public:
42     CallbackDeathRecipient() = default;
43     virtual ~CallbackDeathRecipient() = default;
44     void OnRemoteDied(const wptr<IRemoteObject> &object) override;
45 };
46 
47 class SysEventServiceOhos : public SystemAbility,
48                             public SysEventServiceStub {
49     DECLARE_SYSTEM_ABILITY(SysEventServiceOhos);
50 public:
51     DISALLOW_COPY_AND_MOVE(SysEventServiceOhos);
SysEventServiceOhos()52     SysEventServiceOhos()
53         : deathRecipient_(new CallbackDeathRecipient()), dataPublisher_(new DataPublisher()){};
54     virtual ~SysEventServiceOhos();
55 
56     static sptr<SysEventServiceOhos> GetInstance();
57     static void StartService(SysEventServiceBase* service);
58     static SysEventServiceBase* GetSysEventService(SysEventServiceBase* service = nullptr);
59 
60     /* IPC interface */
61     ErrCode AddListener(
62         const std::vector<SysEventRule>& rules,
63         const OHOS::sptr<ISysEventCallback>& callback) override;
64     ErrCode RemoveListener(
65         const OHOS::sptr<ISysEventCallback>& callback) override;
66     ErrCode Query(
67         const QueryArgument& queryArgument,
68         const std::vector<SysEventQueryRule>& rules,
69         const OHOS::sptr<IQuerySysEventCallback>& callback) override;
70     ErrCode AddSubscriber(
71         const std::vector<SysEventQueryRule>& rules,
72         int64_t& funcResult) override;
73     ErrCode RemoveSubscriber() override;
74     ErrCode Export(
75         const QueryArgument& queryArgument,
76         const std::vector<SysEventQueryRule>& rules,
77         int64_t& funcResult) override;
78 
79     /* SA interface */
80     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
81 
82     void OnSysEvent(std::shared_ptr<SysEvent>& sysEvent);
83     void OnRemoteDied(const wptr<IRemoteObject>& remote);
84     void SetWorkLoop(std::shared_ptr<EventLoop> looper);
85 
86 private:
87     struct ListenerInfo {
88         int32_t pid = 0;
89         int32_t uid = 0;
90         std::vector<SysEventRule> rules;
91     };
92 
93 private:
94     bool HasAccessPermission() const;
95     bool BuildEventQuery(std::shared_ptr<EventQueryWrapperBuilder> builder,
96         const std::vector<SysEventQueryRule>& rules);
97     void MergeEventList(const std::vector<SysEventQueryRule>& rules, std::vector<std::string>& events) const;
98 
99 private:
100     sptr<CallbackDeathRecipient> deathRecipient_;
101     std::mutex listenersMutex_;
102     std::map<OHOS::sptr<OHOS::IRemoteObject>, ListenerInfo> registeredListeners_;
103     std::mutex publisherMutex_;
104     std::shared_ptr<DataPublisher> dataPublisher_;
105 
106 private:
107     static sptr<SysEventServiceOhos> instance_;
108 };
109 } // namespace HiviewDFX
110 } // namespace OHOS
111 
112 #endif // OHOS_HIVIEWDFX_SYS_EVENT_SERVICE_OHOS_H