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 /** 28 * A constructor used to create a CommonEventSubscriber instance 29 * 30 */ 31 CommonEventSubscriber(); 32 33 /** 34 * A constructor used to create a CommonEventSubscriber instance with the 35 * subscribeInfo parameter passed. 36 * 37 * @param subscribeInfo the subscribeInfo 38 */ 39 explicit CommonEventSubscriber(const CommonEventSubscribeInfo &subscribeInfo); 40 41 /** 42 * A deconstructor used to deconstruct 43 * 44 */ 45 virtual ~CommonEventSubscriber(); 46 47 /** 48 * Call back when the application receives a new common event. 49 * 50 * @param data the common event data 51 */ 52 virtual void OnReceiveEvent(const CommonEventData &data) = 0; 53 54 /** 55 * Get common event subscriber info 56 * 57 * @return common event subscriber info 58 */ 59 const CommonEventSubscribeInfo &GetSubscribeInfo() const; 60 61 /** 62 * Set the result code of the current ordered common event. 63 * 64 * @param code the result code of the current ordered common event 65 */ 66 bool SetCode(const int &code); 67 68 /** 69 * Obtain the result code of the current ordered common event. 70 * 71 * @return the result code of the current ordered common event 72 */ 73 int GetCode() const; 74 75 /** 76 * Set the result data of the current ordered common event. 77 * 78 * @param data the result data of the current ordered common event. 79 */ 80 bool SetData(const std::string &data); 81 82 /** 83 * Obtain the result data of the current ordered common event. 84 * 85 * @return the result data of the current ordered common event 86 */ 87 std::string GetData() const; 88 89 /** 90 * Set the result of the current ordered common event. 91 * 92 * @param code the result code of the current ordered common event. 93 * @param data the result data of the current ordered common event. 94 */ 95 bool SetCodeAndData(const int &code, const std::string &data); 96 97 /** 98 * Cancel the current ordered common event. 99 */ 100 bool AbortCommonEvent(); 101 102 /** 103 * Clear the abort state of the current ordered common event. 104 */ 105 bool ClearAbortCommonEvent(); 106 107 /** 108 * Check whether the current ordered common event should be aborted. 109 */ 110 bool GetAbortCommonEvent() const; 111 112 /** 113 * Enable asynchronous processing for the current ordered common event. 114 */ 115 std::shared_ptr<AsyncCommonEventResult> GoAsyncCommonEvent(); 116 117 /** 118 * Check whether the current common event is an ordered common event. 119 */ 120 bool IsOrderedCommonEvent() const; 121 122 /** 123 * Check whether the current common event is a sticky common event. 124 */ 125 bool IsStickyCommonEvent() const; 126 127 private: 128 /** 129 * Set AsyncCommonEventResult for init before perform onReceiveEvent. 130 */ 131 bool SetAsyncCommonEventResult(const std::shared_ptr<AsyncCommonEventResult> &result); 132 133 /** 134 * Get AsyncCommonEventResult for check after perform onReceiveEvent. 135 */ 136 std::shared_ptr<AsyncCommonEventResult> GetAsyncCommonEventResult(); 137 138 /** 139 * Check whether the current common event is ordered. 140 */ 141 bool CheckSynchronous() const; 142 143 private: 144 friend class CommonEventListener; 145 146 CommonEventSubscribeInfo subscribeInfo_; 147 std::shared_ptr<AsyncCommonEventResult> result_; 148 }; 149 } // namespace EventFwk 150 } // namespace OHOS 151 152 #endif // FOUNDATION_EVENT_CESFWK_KITS_NATIVE_INCLUDE_COMMON_EVENT_SUBSCRIBER_H 153