• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FOUNDATION_EVENT_CESFWK_INNERKITS_INCLUDE_COMMON_EVENT_H
17 #define FOUNDATION_EVENT_CESFWK_INNERKITS_INCLUDE_COMMON_EVENT_H
18 
19 #include <map>
20 #include <mutex>
21 
22 #include "common_event_listener.h"
23 #include "icommon_event.h"
24 
25 namespace OHOS {
26 namespace EventFwk {
27 class CommonEvent {
28 public:
29     /**
30      * Publishes a common event.
31      *
32      * @param data Indicates the common event data.
33      * @param publishInfo Indicates the publish info.
34      * @param subscriber Indicates the common event subscriber.
35      * @return Returns true if successful; false otherwise.
36      */
37     bool PublishCommonEvent(const CommonEventData &data, const CommonEventPublishInfo &publishInfo,
38         const std::shared_ptr<CommonEventSubscriber> &subscriber);
39 
40     /**
41      * Publishes a common event.
42      *
43      * @param data Indicates the common event data.
44      * @param publishInfo Indicates the publish info.
45      * @param subscriber Indicates the common event subscriber.
46      * @param userId Indicates the user ID.
47      * @return Returns ERR_OK if successful; otherwise failed.
48      */
49     int32_t PublishCommonEventAsUser(const CommonEventData &data, const CommonEventPublishInfo &publishInfo,
50         const std::shared_ptr<CommonEventSubscriber> &subscriber, const int32_t &userId);
51 
52     /**
53      * Publishes a common event.
54      *
55      * @param data Indicates the common event data.
56      * @param publishInfo Indicates the publish info.
57      * @param subscriber Indicates the common event subscriber.
58      * @param uid Indicates the uid of application.
59      * @param callerToken Indicates the caller token.
60      * @return Returns true if successful; false otherwise.
61      */
62     bool PublishCommonEvent(const CommonEventData &data, const CommonEventPublishInfo &publishInfo,
63         const std::shared_ptr<CommonEventSubscriber> &subscriber, const uid_t &uid, const int32_t &callerToken);
64 
65     /**
66      * Publishes a common event.
67      *
68      * @param data Indicates the common event data.
69      * @param publishInfo Indicates the publish info.
70      * @param subscriber Indicates the common event subscriber.
71      * @param uid Indicates the uid of application.
72      * @param callerToken Indicates the caller token.
73      * @param userId Indicates the user ID.
74      * @return Returns true if successful; false otherwise.
75      */
76     bool PublishCommonEventAsUser(const CommonEventData &data, const CommonEventPublishInfo &publishInfo,
77         const std::shared_ptr<CommonEventSubscriber> &subscriber, const uid_t &uid, const int32_t &callerToken,
78         const int32_t &userId);
79 
80     /**
81      * Subscribes to common events.
82      *
83      * @param subscriber Indicates the common event subscriber.
84      * @return Returns ERR_OK if successful; otherwise failed.
85      */
86     int32_t SubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> &subscriber);
87 
88     /**
89      * Unsubscribes from common events.
90      *
91      * @param subscriber Indicates the common event subscriber.
92      * @return Returns true if successful; false otherwise.
93      */
94     int32_t UnSubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> &subscriber);
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     bool GetStickyCommonEvent(const std::string &event, CommonEventData &eventData);
104 
105     /**
106      * Finishes Receiver.
107      *
108      * @param proxy Indicates the receiver proxy.
109      * @param code Indicates the code of a common event.
110      * @param data Indicates the data of a common event.
111      * @param abortEvent Indicates whether to cancel the current common event.
112      * @return Returns true if successful; false otherwise.
113      */
114     bool FinishReceiver(
115         const sptr<IRemoteObject> &proxy, const int32_t &code, const std::string &data, const bool &abortEvent);
116 
117     /**
118      * Dumps state of common event service.
119      *
120      * @param dumpType Indicates the dump type.
121      * @param event Specifies the information for the common event. Set null string ("") if you want to dump all.
122      * @param userId Indicates the user ID.
123      * @param state Indicates the state of common event service.
124      * @return Returns true if successful; false otherwise.
125      */
126     bool DumpState(const uint8_t &dumpType, const std::string &event, const int32_t &userId,
127         std::vector<std::string> &state);
128 
129     /**
130      * Resets Common Event Proxy.
131      */
132     void ResetCommonEventProxy();
133 
134     /**
135      * Freezes application.
136      *
137      * @param uid Indicates the uid of application.
138      * @return Returns true if successful; false otherwise.
139      */
140     bool Freeze(const uid_t &uid);
141 
142     /**
143      * Unfreezes application.
144      *
145      * @param uid Indicates the uid of application.
146      * @return Returns true if successful; false otherwise.
147      */
148     bool Unfreeze(const uid_t &uid);
149 
150     /**
151      * Unfreezes all frozen applications.
152      *
153      * @return Returns true if successful; false otherwise.
154      */
155     bool UnfreezeAll();
156 private:
157     /**
158      * Gets common event proxy.
159      *
160      * @return Returns get common event proxy success or not.
161      */
162     bool GetCommonEventProxy();
163 
164     /**
165      * Gets common evenet listener.
166      *
167      * @param  subscriber Indicates the subscriber.
168      * @param  commonEventListener Indicates the common event listener.
169      * @return Returns get common event listener success or not.
170      */
171     uint8_t CreateCommonEventListener(
172         const std::shared_ptr<CommonEventSubscriber> &subscriber, sptr<IRemoteObject> &commonEventListener);
173 
174     /**
175      * Parameter checking for publishing public events.
176      *
177      * @param data Indicates the common event data.
178      * @param publishInfo Indicates the publish info.
179      * @param subscriber Indicates the common event subscriber.
180      * @return Returns true if successful; false otherwise.
181      */
182     bool PublishParameterCheck(const CommonEventData &data, const CommonEventPublishInfo &publishInfo,
183         const std::shared_ptr<CommonEventSubscriber> &subscriber, sptr<IRemoteObject> &commonEventListener);
184 
185 private:
186     bool isProxyValid_ = false;
187     std::mutex mutex_;
188     std::mutex eventListenersMutex_;
189     sptr<ICommonEvent> commonEventProxy_;
190     std::map<std::shared_ptr<CommonEventSubscriber>, sptr<CommonEventListener>> eventListeners_;
191     sptr<IRemoteObject::DeathRecipient> recipient_;
192     const size_t SUBSCRIBER_MAX_SIZE = 200;
193     static const uint8_t ALREADY_SUBSCRIBED = 0;
194     static const uint8_t INITIAL_SUBSCRIPTION = 1;
195     static const uint8_t SUBSCRIBE_FAILED = 2;
196 };
197 }  // namespace EventFwk
198 }  // namespace OHOS
199 
200 #endif  // FOUNDATION_EVENT_CESFWK_INNERKITS_INCLUDE_COMMON_EVENT_H