1 /* 2 * Copyright (c) 2021 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_KITS_NATIVE_INCLUDE_COMMON_EVENT_SUBSCRIBER_H 17 #define FOUNDATION_EVENT_CESFWK_KITS_NATIVE_INCLUDE_COMMON_EVENT_SUBSCRIBER_H 18 19 #include "async_common_event_result.h" 20 #include "common_event_data.h" 21 #include "common_event_subscribe_info.h" 22 23 namespace OHOS { 24 namespace EventFwk { 25 class CommonEventSubscriber { 26 public: 27 CommonEventSubscriber(); 28 29 /** 30 * A constructor used to create a CommonEventSubscriber instance with the 31 * subscribeInfo parameter passed. 32 * 33 * @param subscribeInfo Indicates the subscribeInfo 34 */ 35 explicit CommonEventSubscriber(const CommonEventSubscribeInfo &subscribeInfo); 36 37 virtual ~CommonEventSubscriber(); 38 39 /** 40 * Calls back when the application receives a new common event. 41 * Don't call other common event interface such as UnSubscribeCommonEvent() in the callback 42 * 43 * @param data Indicates the common event data. 44 */ 45 virtual void OnReceiveEvent(const CommonEventData &data) = 0; 46 47 /** 48 * Gets common event subscriber info 49 * 50 * @return Returns common event subscriber info 51 */ 52 const CommonEventSubscribeInfo &GetSubscribeInfo() const; 53 54 /** 55 * Sets the result code of the current ordered common event. 56 * 57 * @param code Indicates the result code of the current ordered common event 58 * @return Returns true if success; false otherwise. 59 */ 60 bool SetCode(const int32_t &code); 61 62 /** 63 * Obtains the result code of the current ordered common event. 64 * 65 * @return Returns the result code of the current ordered common event. 66 */ 67 int32_t GetCode() const; 68 69 /** 70 * Sets the result data of the current ordered common event. 71 * 72 * @param data Indicates the result data of the current ordered common event. 73 * @return Returns true if success; false otherwise. 74 */ 75 bool SetData(const std::string &data); 76 77 /** 78 * Obtains the result data of the current ordered common event. 79 * 80 * @return Returns the result data of the current ordered common event 81 */ 82 std::string GetData() const; 83 84 /** 85 * Sets the result of the current ordered common event. 86 * 87 * @param code Indicates the result code of the current ordered common event. 88 * @param data Indicates the result data of the current ordered common event. 89 * @return Returns true if success; false otherwise. 90 */ 91 bool SetCodeAndData(const int32_t &code, const std::string &data); 92 93 /** 94 * Cancels the current ordered common event. 95 * 96 * @return Returns true if success; false otherwise. 97 */ 98 bool AbortCommonEvent(); 99 100 /** 101 * Clears the abort state of the current ordered common event. 102 * 103 * @return Returns true if success; false otherwise. 104 */ 105 bool ClearAbortCommonEvent(); 106 107 /** 108 * Checks whether the current ordered common event should be aborted. 109 * 110 * @return Returns true if success; false otherwise. 111 */ 112 bool GetAbortCommonEvent() const; 113 114 /** 115 * Enables asynchronous processing for the current ordered common event. 116 * @return Returns async common event result. 117 */ 118 std::shared_ptr<AsyncCommonEventResult> GoAsyncCommonEvent(); 119 120 /** 121 * Checks whether the current common event is an ordered common event. 122 123 * @return Returns true if success; false otherwise. 124 */ 125 bool IsOrderedCommonEvent() const; 126 127 /** 128 * Checks whether the current common event is a sticky common event. 129 * 130 * @return Returns true if success; false otherwise. 131 */ 132 bool IsStickyCommonEvent() const; 133 134 private: 135 /** 136 * Sets AsyncCommonEventResult for init before perform onReceiveEvent. 137 * 138 * @return Returns true if success; false otherwise. 139 */ 140 bool SetAsyncCommonEventResult(const std::shared_ptr<AsyncCommonEventResult> &result); 141 142 /** 143 * Gets AsyncCommonEventResult for check after perform onReceiveEvent. 144 * 145 * @return Returns async common event result. 146 */ 147 std::shared_ptr<AsyncCommonEventResult> GetAsyncCommonEventResult(); 148 149 /** 150 * Checks whether the current common event is ordered. 151 * 152 * @return Returns true if success; false otherwise. 153 */ 154 bool CheckSynchronous() const; 155 156 private: 157 friend class CommonEventListener; 158 159 CommonEventSubscribeInfo subscribeInfo_; 160 std::shared_ptr<AsyncCommonEventResult> result_; 161 }; 162 } // namespace EventFwk 163 } // namespace OHOS 164 165 #endif // FOUNDATION_EVENT_CESFWK_KITS_NATIVE_INCLUDE_COMMON_EVENT_SUBSCRIBER_H 166