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