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