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