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