• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_listener.h"
17 #include "event_log_wrapper.h"
18 #include "hitrace_meter.h"
19 
20 namespace OHOS {
21 namespace EventFwk {
22 std::shared_ptr<AppExecFwk::EventRunner> CommonEventListener::commonRunner_ = nullptr;
23 
CommonEventListener(const std::shared_ptr<CommonEventSubscriber> & commonEventSubscriber)24 CommonEventListener::CommonEventListener(const std::shared_ptr<CommonEventSubscriber> &commonEventSubscriber)
25     : commonEventSubscriber_(commonEventSubscriber)
26 {
27     Init();
28 }
29 
~CommonEventListener()30 CommonEventListener::~CommonEventListener()
31 {}
32 
NotifyEvent(const CommonEventData & commonEventData,const bool & ordered,const bool & sticky)33 void CommonEventListener::NotifyEvent(const CommonEventData &commonEventData, const bool &ordered, const bool &sticky)
34 {
35     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
36     EVENT_LOGD("enter");
37 
38     std::lock_guard<std::mutex> lock(mutex_);
39     if (!IsReady()) {
40         EVENT_LOGE("not ready");
41         return;
42     }
43 
44     wptr<CommonEventListener> wp = this;
45     std::function<void()> onReceiveEventFunc = [wp, commonEventData, ordered, sticky] () {
46         sptr<CommonEventListener> sThis = wp.promote();
47         if (sThis == nullptr) {
48             EVENT_LOGE("invalid listener");
49             return;
50         }
51         sThis->OnReceiveEvent(commonEventData, ordered, sticky);
52     };
53     handler_->PostTask(onReceiveEventFunc);
54 }
55 
Init()56 ErrCode CommonEventListener::Init()
57 {
58     EVENT_LOGD("ready to init");
59 
60     std::lock_guard<std::mutex> lock(mutex_);
61     if (runner_ == nullptr) {
62         if (!commonEventSubscriber_) {
63             EVENT_LOGE("Failed to init with CommonEventSubscriber nullptr");
64             return ERR_INVALID_OPERATION;
65         }
66 
67         auto threadMode = commonEventSubscriber_->GetSubscribeInfo().GetThreadMode();
68         EVENT_LOGD("thread mode: %{public}d", threadMode);
69         if (threadMode == CommonEventSubscribeInfo::HANDLER) {
70             runner_ = EventRunner::GetMainEventRunner();
71         } else if (threadMode == CommonEventSubscribeInfo::COMMON) {
72             runner_ = GetCommonRunner();
73         } else {
74             runner_ = GetCommonRunner();
75         }
76         if (!runner_) {
77             EVENT_LOGE("Failed to init due to create runner error");
78             return ERR_INVALID_OPERATION;
79         }
80     }
81 
82     if (handler_ == nullptr) {
83         handler_ = std::make_shared<EventHandler>(runner_);
84         if (!handler_) {
85             EVENT_LOGE("Failed to init due to create handler error");
86             return ERR_INVALID_OPERATION;
87         }
88     }
89 
90     return ERR_OK;
91 }
92 
GetCommonRunner()93 std::shared_ptr<AppExecFwk::EventRunner> CommonEventListener::GetCommonRunner()
94 {
95     if (CommonEventListener::commonRunner_ == nullptr) {
96         CommonEventListener::commonRunner_ = EventRunner::Create("CesComListener");
97     }
98 
99     return CommonEventListener::commonRunner_;
100 }
101 
IsReady()102 bool CommonEventListener::IsReady()
103 {
104     if (runner_ == nullptr) {
105         EVENT_LOGE("runner is not ready");
106         return false;
107     }
108 
109     if (handler_ == nullptr) {
110         EVENT_LOGE("handler is not ready");
111         return false;
112     }
113 
114     return true;
115 }
116 
OnReceiveEvent(const CommonEventData & commonEventData,const bool & ordered,const bool & sticky)117 void CommonEventListener::OnReceiveEvent(
118     const CommonEventData &commonEventData, const bool &ordered, const bool &sticky)
119 {
120     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
121     EVENT_LOGD("enter %{public}s", commonEventData.GetWant().GetAction().c_str());
122 
123     std::lock_guard<std::mutex> lock(mutex_);
124 
125     int32_t code = commonEventData.GetCode();
126     std::string data = commonEventData.GetData();
127 
128     std::shared_ptr<AsyncCommonEventResult> result =
129         std::make_shared<AsyncCommonEventResult>(code, data, ordered, sticky, this);
130     if (result == nullptr) {
131         EVENT_LOGE("Failed to create AsyncCommonEventResult");
132         return;
133     }
134 
135     if (!commonEventSubscriber_) {
136         EVENT_LOGE("CommonEventSubscriber ptr is nullptr");
137         return;
138     }
139     commonEventSubscriber_->SetAsyncCommonEventResult(result);
140 
141     commonEventSubscriber_->OnReceiveEvent(commonEventData);
142 
143     if ((commonEventSubscriber_->GetAsyncCommonEventResult() != nullptr) && ordered) {
144         commonEventSubscriber_->GetAsyncCommonEventResult()->FinishCommonEvent();
145     }
146     EVENT_LOGD("end");
147 }
148 
Stop()149 void CommonEventListener::Stop()
150 {
151     EVENT_LOGD("enter");
152     std::lock_guard<std::mutex> lock(mutex_);
153     if (handler_) {
154         handler_.reset();
155     }
156 
157     if (commonEventSubscriber_ == nullptr) {
158         EVENT_LOGE("commonEventSubscriber_ == nullptr");
159         return;
160     }
161 
162     if (CommonEventSubscribeInfo::HANDLER == commonEventSubscriber_->GetSubscribeInfo().GetThreadMode()) {
163         EVENT_LOGD("stop listener in HANDLER mode");
164         return;
165     }
166 
167     if (runner_) {
168         runner_.reset();
169     }
170 }
171 }  // namespace EventFwk
172 }  // namespace OHOS
173