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 * 42 * @param data Indicates the common event data. 43 */ 44 virtual void OnReceiveEvent(const CommonEventData &data) = 0; 45 46 /** 47 * Gets common event subscriber info 48 * 49 * @return Returns common event subscriber info 50 */ 51 const CommonEventSubscribeInfo &GetSubscribeInfo() const; 52 53 /** 54 * Sets the result code of the current ordered common event. 55 * 56 * @param code Indicates the result code of the current ordered common event 57 * @return Returns true if success; false otherwise. 58 */ 59 bool SetCode(const int32_t &code); 60 61 /** 62 * Obtains the result code of the current ordered common event. 63 * 64 * @return Returns the result code of the current ordered common event. 65 */ 66 int32_t GetCode() const; 67 68 /** 69 * Sets the result data of the current ordered common event. 70 * 71 * @param data Indicates the result data of the current ordered common event. 72 * @return Returns true if success; false otherwise. 73 */ 74 bool SetData(const std::string &data); 75 76 /** 77 * Obtains the result data of the current ordered common event. 78 * 79 * @return Returns the result data of the current ordered common event 80 */ 81 std::string GetData() const; 82 83 /** 84 * Sets the result of the current ordered common event. 85 * 86 * @param code Indicates the result code of the current ordered common event. 87 * @param data Indicates the result data of the current ordered common event. 88 * @return Returns true if success; false otherwise. 89 */ 90 bool SetCodeAndData(const int32_t &code, const std::string &data); 91 92 /** 93 * Cancels the current ordered common event. 94 * 95 * @return Returns true if success; false otherwise. 96 */ 97 bool AbortCommonEvent(); 98 99 /** 100 * Clears the abort state of the current ordered common event. 101 * 102 * @return Returns true if success; false otherwise. 103 */ 104 bool ClearAbortCommonEvent(); 105 106 /** 107 * Checks whether the current ordered common event should be aborted. 108 * 109 * @return Returns true if success; false otherwise. 110 */ 111 bool GetAbortCommonEvent() const; 112 113 /** 114 * Enables asynchronous processing for the current ordered common event. 115 * @return Returns async common event result. 116 */ 117 std::shared_ptr<AsyncCommonEventResult> GoAsyncCommonEvent(); 118 119 /** 120 * Checks whether the current common event is an ordered common event. 121 122 * @return Returns true if success; false otherwise. 123 */ 124 bool IsOrderedCommonEvent() const; 125 126 /** 127 * Checks whether the current common event is a sticky common event. 128 * 129 * @return Returns true if success; false otherwise. 130 */ 131 bool IsStickyCommonEvent() const; 132 133 private: 134 /** 135 * Sets AsyncCommonEventResult for init before perform onReceiveEvent. 136 * 137 * @return Returns true if success; false otherwise. 138 */ 139 bool SetAsyncCommonEventResult(const std::shared_ptr<AsyncCommonEventResult> &result); 140 141 /** 142 * Gets AsyncCommonEventResult for check after perform onReceiveEvent. 143 * 144 * @return Returns async common event result. 145 */ 146 std::shared_ptr<AsyncCommonEventResult> GetAsyncCommonEventResult(); 147 148 /** 149 * Checks whether the current common event is ordered. 150 * 151 * @return Returns true if success; false otherwise. 152 */ 153 bool CheckSynchronous() const; 154 155 private: 156 friend class CommonEventListener; 157 158 CommonEventSubscribeInfo subscribeInfo_; 159 std::shared_ptr<AsyncCommonEventResult> result_; 160 }; 161 } // namespace EventFwk 162 } // namespace OHOS 163 164 #endif // FOUNDATION_EVENT_CESFWK_KITS_NATIVE_INCLUDE_COMMON_EVENT_SUBSCRIBER_H 165