• 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 FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_MANAGER_SERVICE_H
17 #define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_MANAGER_SERVICE_H
18 
19 #include "common_event_stub.h"
20 #include "event_handler.h"
21 #include "ffrt.h"
22 #include "inner_common_event_manager.h"
23 #include "nocopyable.h"
24 #include "refbase.h"
25 #include <mutex>
26 
27 namespace OHOS {
28 namespace EventFwk {
29 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
30 
31 class CommonEventManagerService : public CommonEventStub {
32 public:
33     static sptr<CommonEventManagerService> GetInstance();
34     CommonEventManagerService();
35     virtual ~CommonEventManagerService();
36     /**
37      * Publishes a common event.
38      *
39      * @param event Indicates the common event data.
40      * @param publishInfo Indicates the publish info.
41      * @param commonEventListener Indicates the common event subscriber object.
42      * @param userId Indicates the user ID.
43      * @return Returns true if successful; false otherwise.
44      */
45     ErrCode PublishCommonEvent(const CommonEventData& event, const CommonEventPublishInfo& publishInfo,
46         const sptr<IRemoteObject>& commonEventListener, int32_t userId, int32_t& funcResult) override;
47 
48     ErrCode PublishCommonEvent(const CommonEventData& event, const CommonEventPublishInfo& publishInfo, int32_t userId,
49         int32_t& funcResult) override;
50 
51     /**
52      * Publishes a common event.
53      *
54      * @param event Indicates the common event data.
55      * @param publishInfo Indicates the publish info.
56      * @param commonEventListener Indicates the common event subscriber.
57      * @param uid Indicates the uid of application.
58      * @param callerToken Indicates the caller token
59      * @param userId Indicates the user ID.
60      * @return Returns true if successful; false otherwise.
61      */
62     ErrCode PublishCommonEvent(const CommonEventData& event, const CommonEventPublishInfo& publishInfo,
63         const sptr<IRemoteObject>& commonEventListener, uint32_t uid, int32_t callerToken, int32_t userId,
64         bool& funcResult) override;
65 
66     ErrCode PublishCommonEvent(const CommonEventData& event, const CommonEventPublishInfo& publishInfo, uint32_t uid,
67         int32_t callerToken, int32_t userId, bool& funcResult) override;
68 
69     /**
70      * Subscribes to common events.
71      *
72      * @param subscribeInfo Indicates the subscribe info.
73      * @param commonEventListener Indicates the common event subscriber.
74      * @param instanceKey Indicates the instance key
75      * @return Returns true if successful; false otherwise.
76      */
77     ErrCode SubscribeCommonEvent(const CommonEventSubscribeInfo& subscribeInfo,
78         const sptr<IRemoteObject>& commonEventListener, int32_t instanceKey, int32_t& funcResult) override;
79 
80     /**
81      * Unsubscribes from common events.
82      *
83      * @param commonEventListener Indicates the common event subscriber.
84      * @return Returns true if successful; false otherwise.
85      */
86     ErrCode UnsubscribeCommonEvent(const sptr<IRemoteObject>& commonEventListener, int32_t& funcResult) override;
87 
88     /**
89      * Synchronized, unsubscribes from common events.
90      *
91      * @param commonEventListener Indicates the common event subscriber.
92      * @return Returns true if successful; false otherwise.
93      */
94     ErrCode UnsubscribeCommonEventSync(const sptr<IRemoteObject>& commonEventListener, int32_t& funcResult) override;
95 
96     /**
97      * Gets the current sticky common event
98      *
99      * @param event Indicates the common event.
100      * @param eventData Indicates the common event data.
101      * @return Returns true if successful; false otherwise.
102      */
103     ErrCode GetStickyCommonEvent(const std::string& event, CommonEventData& eventData, bool& funcResult) override;
104 
105     /**
106      * Dumps state of common event service.
107      *
108      * @param dumpType Indicates the dump type.
109      * @param event Specifies the information for the common event. Set null string ("") if you want to dump all.
110      * @param userId Indicates the user ID.
111      * @param state Indicates the state of common event service.
112      * @return Returns true if successful; false otherwise.
113      */
114     ErrCode DumpState(uint8_t dumpType, const std::string& event, int32_t userId, std::vector<std::string>& state,
115         bool& funcResult) override;
116 
117     /**
118      * Finishes Receiver.
119      *
120      * @param proxy Indicates the receiver proxy.
121      * @param code Indicates the code of a common event.
122      * @param data Indicates the data of a common event.
123      * @param abortEvent Indicates Whether to cancel the current common event.
124      * @return Returns true if successful; false otherwise.
125      */
126     ErrCode FinishReceiver(const sptr<IRemoteObject>& proxy, int32_t code, const std::string& receiverData,
127         bool abortEvent, bool& funcResult) override;
128 
129     /**
130      * Freezes application.
131      *
132      * @param uid Indicates the uid of application.
133      * @return Returns true if successful; false otherwise.
134      */
135     ErrCode Freeze(uint32_t uid, bool& funcResult) override;
136 
137     /**
138      * Unfreezes application.
139      *
140      * @param uid Indicates the Uid of application.
141      * @return Returns true if successful; false otherwise.
142      */
143     ErrCode Unfreeze(uint32_t uid, bool& funcResult) override;
144 
145     /**
146      * Unfreezes all frozen applications.
147      *
148      * @return Returns true if successful; false otherwise.
149      */
150     ErrCode UnfreezeAll(bool& funcResult) override;
151 
152     /**
153      * Remove sticky common event.
154      *
155      * @param event Name of the common event.
156      * @return Returns ERR_OK if success; otherwise failed.
157      */
158     ErrCode RemoveStickyCommonEvent(const std::string& event, int32_t& funcResult) override;
159 
160     /**
161      * Set Static Subscriber State.
162      *
163      * @param enable static subscriber state.
164      * @return Returns ERR_OK if success; otherwise failed.
165      */
166     ErrCode SetStaticSubscriberState(bool enable, int32_t& funcResult) override;
167 
168     /**
169      * Set static subscriber state.
170      *
171      * @param events Static subscriber event name.
172      * @param enable Static subscriber state.
173      * @return Returns ERR_OK if success; otherwise failed.
174      */
175     ErrCode SetStaticSubscriberStateByEvents(const std::vector<std::string>& events, bool enable, int32_t& funcResult)
176         override;
177 
178     /**
179     * Set freeze status of process.
180     *
181     * @param pidList Indicates the list of process id.
182     * @param isFreeze Indicates wheather the process is freezed.
183     * @return Returns true if successful; false otherwise.
184     */
185     ErrCode SetFreezeStatus(const std::set<int32_t>& pidList, bool isFreeze, bool& funcResult) override;
186 
187     int Dump(int fd, const std::vector<std::u16string> &args) override;
188 
189     ErrCode Init();
190 
191 private:
192     bool IsReady() const;
193 
194     int32_t PublishCommonEventDetailed(const CommonEventData &event, const CommonEventPublishInfo &publishinfo,
195         const sptr<IRemoteObject> &commonEventListener, const pid_t &pid, const uid_t &uid,
196         const int32_t &clientToken, const int32_t &userId);
197 
198     void GetHidumpInfo(const std::vector<std::u16string> &args, std::string &result);
199     int32_t CheckUserIdParams(const int32_t &userId);
200 private:
201     static sptr<CommonEventManagerService> instance_;
202     static ffrt::mutex instanceMutex_;
203 
204     std::shared_ptr<InnerCommonEventManager> innerCommonEventManager_;
205     ServiceRunningState serviceRunningState_ = ServiceRunningState::STATE_NOT_START;
206     std::shared_ptr<EventRunner> runner_;
207     std::shared_ptr<EventHandler> handler_;
208     std::shared_ptr<ffrt::queue> commonEventSrvQueue_ = nullptr;
209     std::string supportCheckSaPermission_ = "false";
210 
211     DISALLOW_COPY_AND_MOVE(CommonEventManagerService);
212 };
213 }  // namespace EventFwk
214 }  // namespace OHOS
215 
216 #endif  // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_MANAGER_SERVICE_H