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 "async_common_event_result.h"
17 #include "common_event.h"
18 #include "event_log_wrapper.h"
19 #include "singleton.h"
20
21 namespace OHOS {
22 namespace EventFwk {
AsyncCommonEventResult(const int32_t & resultCode,const std::string & resultData,const bool & ordered,const bool & sticky,const sptr<IRemoteObject> & token)23 AsyncCommonEventResult::AsyncCommonEventResult(const int32_t &resultCode, const std::string &resultData,
24 const bool &ordered, const bool &sticky, const sptr<IRemoteObject> &token)
25 : abortEvent_(false), finished_(false)
26 {
27 resultCode_ = resultCode;
28 resultData_ = resultData;
29 ordered_ = ordered;
30 sticky_ = sticky;
31 token_ = token;
32 }
33
~AsyncCommonEventResult()34 AsyncCommonEventResult::~AsyncCommonEventResult()
35 {}
36
SetCode(const int32_t & code)37 bool AsyncCommonEventResult::SetCode(const int32_t &code)
38 {
39 if (!CheckSynchronous()) {
40 EVENT_LOGE("failed to CheckSynchronous");
41 return false;
42 }
43
44 resultCode_ = code;
45
46 return true;
47 }
48
GetCode() const49 int32_t AsyncCommonEventResult::GetCode() const
50 {
51 return resultCode_;
52 }
53
SetData(const std::string & data)54 bool AsyncCommonEventResult::SetData(const std::string &data)
55 {
56 if (!CheckSynchronous()) {
57 EVENT_LOGE("failed to CheckSynchronous");
58 return false;
59 }
60
61 resultData_ = data;
62
63 return true;
64 }
65
GetData() const66 std::string AsyncCommonEventResult::GetData() const
67 {
68 return resultData_;
69 }
70
SetCodeAndData(const int32_t & code,const std::string & data)71 bool AsyncCommonEventResult::SetCodeAndData(const int32_t &code, const std::string &data)
72 {
73 if (!CheckSynchronous()) {
74 EVENT_LOGE("failed to CheckSynchronous");
75 return false;
76 }
77
78 resultCode_ = code;
79 resultData_ = data;
80
81 return true;
82 }
83
AbortCommonEvent()84 bool AsyncCommonEventResult::AbortCommonEvent()
85 {
86 if (!CheckSynchronous()) {
87 EVENT_LOGE("failed to CheckSynchronous");
88 return false;
89 }
90
91 abortEvent_ = true;
92
93 return true;
94 }
95
ClearAbortCommonEvent()96 bool AsyncCommonEventResult::ClearAbortCommonEvent()
97 {
98 if (!CheckSynchronous()) {
99 EVENT_LOGE("failed to CheckSynchronous");
100 return false;
101 }
102
103 abortEvent_ = false;
104
105 return true;
106 }
107
GetAbortCommonEvent() const108 bool AsyncCommonEventResult::GetAbortCommonEvent() const
109 {
110 return abortEvent_;
111 }
112
FinishCommonEvent()113 bool AsyncCommonEventResult::FinishCommonEvent()
114 {
115 EVENT_LOGD("enter");
116
117 if (!CheckSynchronous()) {
118 EVENT_LOGE("failed to CheckSynchronous");
119 return false;
120 }
121
122 if (finished_) {
123 EVENT_LOGE("Common event already finished.");
124 return false;
125 }
126 finished_ = true;
127
128 return DelayedSingleton<CommonEvent>::GetInstance()->FinishReceiver(token_, resultCode_, resultData_, abortEvent_);
129 }
130
IsOrderedCommonEvent() const131 bool AsyncCommonEventResult::IsOrderedCommonEvent() const
132 {
133 return ordered_;
134 }
135
IsStickyCommonEvent() const136 bool AsyncCommonEventResult::IsStickyCommonEvent() const
137 {
138 return sticky_;
139 }
140
CheckSynchronous() const141 bool AsyncCommonEventResult::CheckSynchronous() const
142 {
143 bool ret = false;
144
145 if (ordered_) {
146 ret = true;
147 } else {
148 EVENT_LOGE("Subscriber want to set result for an unordered common event.");
149 return false;
150 }
151
152 return ret;
153 }
154 } // namespace EventFwk
155 } // namespace OHOS
156