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 #include "common_event_subscriber.h" 17 #include "event_log_wrapper.h" 18 19 namespace OHOS { 20 namespace EventFwk { CommonEventSubscriber()21CommonEventSubscriber::CommonEventSubscriber() : result_(nullptr) 22 { 23 EVENT_LOGD("constructor CommonEventSubscriber"); 24 } 25 CommonEventSubscriber(const CommonEventSubscribeInfo & subscribeInfo)26CommonEventSubscriber::CommonEventSubscriber(const CommonEventSubscribeInfo &subscribeInfo) 27 : subscribeInfo_(subscribeInfo), result_(nullptr) 28 { 29 EVENT_LOGD("constructor CommonEventSubscriber"); 30 } 31 ~CommonEventSubscriber()32CommonEventSubscriber::~CommonEventSubscriber() 33 { 34 EVENT_LOGD("destructor CommonEventSubscriber"); 35 } 36 GetSubscribeInfo() const37const CommonEventSubscribeInfo &CommonEventSubscriber::GetSubscribeInfo() const 38 { 39 return subscribeInfo_; 40 } 41 SetCode(const int32_t & code)42bool CommonEventSubscriber::SetCode(const int32_t &code) 43 { 44 if (!CheckSynchronous()) { 45 EVENT_LOGE("failed to CheckSynchronous"); 46 return false; 47 } 48 49 return result_->SetCode(code); 50 } 51 GetCode() const52int32_t CommonEventSubscriber::GetCode() const 53 { 54 if (!CheckSynchronous()) { 55 EVENT_LOGE("failed to CheckSynchronous"); 56 return 0; 57 } 58 59 return result_->GetCode(); 60 } 61 SetData(const std::string & data)62bool CommonEventSubscriber::SetData(const std::string &data) 63 { 64 if (!CheckSynchronous()) { 65 EVENT_LOGE("failed to CheckSynchronous"); 66 return false; 67 } 68 69 return result_->SetData(data); 70 } 71 GetData() const72std::string CommonEventSubscriber::GetData() const 73 { 74 if (!CheckSynchronous()) { 75 EVENT_LOGE("failed to CheckSynchronous"); 76 return std::string(); 77 } 78 79 return result_->GetData(); 80 } 81 SetCodeAndData(const int32_t & code,const std::string & data)82bool CommonEventSubscriber::SetCodeAndData(const int32_t &code, const std::string &data) 83 { 84 if (!CheckSynchronous()) { 85 EVENT_LOGE("failed to CheckSynchronous"); 86 return false; 87 } 88 89 return result_->SetCodeAndData(code, data); 90 } 91 AbortCommonEvent()92bool CommonEventSubscriber::AbortCommonEvent() 93 { 94 if (!CheckSynchronous()) { 95 EVENT_LOGE("failed to CheckSynchronous"); 96 return false; 97 } 98 99 return result_->AbortCommonEvent(); 100 } 101 ClearAbortCommonEvent()102bool CommonEventSubscriber::ClearAbortCommonEvent() 103 { 104 if (!CheckSynchronous()) { 105 EVENT_LOGE("failed to CheckSynchronous"); 106 return false; 107 } 108 109 return result_->ClearAbortCommonEvent(); 110 } 111 GetAbortCommonEvent() const112bool CommonEventSubscriber::GetAbortCommonEvent() const 113 { 114 if (!CheckSynchronous()) { 115 EVENT_LOGE("failed to CheckSynchronous"); 116 return false; 117 } 118 119 return result_->GetAbortCommonEvent(); 120 } 121 GoAsyncCommonEvent()122std::shared_ptr<AsyncCommonEventResult> CommonEventSubscriber::GoAsyncCommonEvent() 123 { 124 std::shared_ptr<AsyncCommonEventResult> res = result_; 125 result_ = nullptr; 126 return res; 127 } 128 IsOrderedCommonEvent() const129bool CommonEventSubscriber::IsOrderedCommonEvent() const 130 { 131 return (result_ != nullptr) ? result_->IsOrderedCommonEvent() : false; 132 } 133 IsStickyCommonEvent() const134bool CommonEventSubscriber::IsStickyCommonEvent() const 135 { 136 return (result_ != nullptr) ? result_->IsStickyCommonEvent() : false; 137 } 138 SetAsyncCommonEventResult(const std::shared_ptr<AsyncCommonEventResult> & result)139bool CommonEventSubscriber::SetAsyncCommonEventResult(const std::shared_ptr<AsyncCommonEventResult> &result) 140 { 141 result_ = result; 142 143 return true; 144 } 145 GetAsyncCommonEventResult()146std::shared_ptr<AsyncCommonEventResult> CommonEventSubscriber::GetAsyncCommonEventResult() 147 { 148 return result_; 149 } 150 CheckSynchronous() const151bool CommonEventSubscriber::CheckSynchronous() const 152 { 153 if (!result_) { 154 EVENT_LOGE("Call when result is not set."); 155 return false; 156 } 157 if (!result_->CheckSynchronous()) { 158 return false; 159 } 160 161 return true; 162 } 163 } // namespace EventFwk 164 } // namespace OHOS