1 /*
2 * Copyright (c) 2022-2025 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 #include "app_event_observer_mgr.h"
16
17 #include "app_state_callback.h"
18 #include "app_event_processor_proxy.h"
19 #include "app_event_store.h"
20 #include "app_event_watcher.h"
21 #include "application_context.h"
22 #include "ffrt_inner.h"
23 #include "hiappevent_base.h"
24 #include "hiappevent_config.h"
25 #include "hilog/log.h"
26 #include "os_event_listener.h"
27
28 #undef LOG_DOMAIN
29 #define LOG_DOMAIN 0xD002D07
30
31 #undef LOG_TAG
32 #define LOG_TAG "ObserverMgr"
33
34 namespace OHOS {
35 namespace HiviewDFX {
36 using HiAppEvent::AppEventFilter;
37 using HiAppEvent::TriggerCondition;
38 using namespace AppEventCacheCommon;
39 namespace {
40 constexpr int MILLI_TO_MICRO = 1000;
41 constexpr int REFRESH_FREE_SIZE_INTERVAL = 10 * 60 * 1000; // 10 minutes
42 constexpr int TIMEOUT_INTERVAL_MILLI = HiAppEvent::TIMEOUT_STEP * 1000; // 30s
43 constexpr int MAX_SIZE_OF_INIT = 100;
44 constexpr int TIMEOUT_LIMIT_FOR_ADDPROCESSOR = 500;
45 constexpr int CHECK_DB_INTERVAL = 1;
46
StoreEventsToDb(std::vector<std::shared_ptr<AppEventPack>> & events)47 void StoreEventsToDb(std::vector<std::shared_ptr<AppEventPack>>& events)
48 {
49 for (auto& event : events) {
50 int64_t eventSeq = AppEventStore::GetInstance().InsertEvent(event);
51 if (eventSeq <= 0) {
52 HILOG_WARN(LOG_CORE, "failed to store event to db");
53 continue;
54 }
55 event->SetSeq(eventSeq);
56 AppEventStore::GetInstance().QueryCustomParamsAdd2EventPack(event);
57 }
58 }
59
StoreEventMappingToDb(const std::vector<std::shared_ptr<AppEventPack>> & events,const std::vector<std::shared_ptr<AppEventObserver>> & observers)60 void StoreEventMappingToDb(const std::vector<std::shared_ptr<AppEventPack>>& events,
61 const std::vector<std::shared_ptr<AppEventObserver>>& observers)
62 {
63 std::vector<EventObserverInfo> eventObserverInfos;
64 for (const auto& observer : observers) {
65 for (const auto& event : events) {
66 if (observer->VerifyEvent(event)) {
67 eventObserverInfos.emplace_back(EventObserverInfo(event->GetSeq(), observer->GetSeq()));
68 }
69 }
70 }
71 if (AppEventStore::GetInstance().InsertEventMapping(eventObserverInfos) < 0) {
72 HILOG_ERROR(LOG_CORE, "failed to add mapping record to db");
73 }
74 }
75
SendEventsToObserver(const std::vector<std::shared_ptr<AppEventPack>> & events,std::shared_ptr<AppEventObserver> observer)76 void SendEventsToObserver(const std::vector<std::shared_ptr<AppEventPack>>& events,
77 std::shared_ptr<AppEventObserver> observer)
78 {
79 std::vector<std::shared_ptr<AppEventPack>> realTimeEvents;
80 for (const auto& event : events) {
81 if (!observer->VerifyEvent(event)) {
82 continue;
83 }
84 if (observer->IsRealTimeEvent(event)) {
85 realTimeEvents.emplace_back(event);
86 } else {
87 observer->ProcessEvent(event);
88 }
89 }
90 if (!realTimeEvents.empty()) {
91 observer->OnEvents(realTimeEvents);
92 }
93 }
94
StoreObserverToDb(std::shared_ptr<AppEventObserver> observer,const std::string & filters,int64_t hashCode)95 int64_t StoreObserverToDb(std::shared_ptr<AppEventObserver> observer, const std::string& filters, int64_t hashCode)
96 {
97 std::string name = observer->GetName();
98 int64_t observerSeq = AppEventStore::GetInstance().InsertObserver(Observer(name, hashCode, filters));
99 if (observerSeq <= 0) {
100 HILOG_ERROR(LOG_CORE, "failed to insert observer=%{public}s to db", name.c_str());
101 return -1;
102 }
103 observer->SetSeq(observerSeq);
104 return observerSeq;
105 }
106
InitObserverFromDb(std::shared_ptr<AppEventObserver> observer,const std::string & filters,int64_t hashCode=0)107 int64_t InitObserverFromDb(std::shared_ptr<AppEventObserver> observer, const std::string& filters, int64_t hashCode = 0)
108 {
109 int64_t observerSeq = observer->GetSeq();
110 if (observerSeq <= 0) {
111 HILOG_INFO(LOG_CORE, "the observer does not exist in database, name=%{public}s, hash=%{public}" PRId64,
112 observer->GetName().c_str(), hashCode);
113 return StoreObserverToDb(observer, filters, hashCode);
114 }
115 std::vector<std::shared_ptr<AppEventPack>> events;
116 if (AppEventStore::GetInstance().QueryEvents(events, observerSeq, MAX_SIZE_OF_INIT) < 0) {
117 HILOG_ERROR(LOG_CORE, "failed to take events, seq=%{public}" PRId64, observerSeq);
118 return -1;
119 }
120 if (!events.empty()) {
121 if (hashCode == 0) {
122 // send old events to watcher where init
123 SendEventsToObserver(events, observer);
124 } else {
125 TriggerCondition triggerCond;
126 for (auto event : events) {
127 triggerCond.row++;
128 triggerCond.size += static_cast<int>(event->GetEventStr().size());
129 }
130 observer->SetCurrCondition(triggerCond);
131 }
132 }
133 return observerSeq;
134 }
135 }
136
GetInstance()137 AppEventObserverMgr& AppEventObserverMgr::GetInstance()
138 {
139 static AppEventObserverMgr instance;
140 return instance;
141 }
142
AppEventObserverMgr()143 AppEventObserverMgr::AppEventObserverMgr()
144 {
145 RegisterAppStateCallback();
146 moduleLoader_ = std::make_unique<ModuleLoader>();
147 queue_ = std::make_shared<ffrt::queue>("AppEventQueue");
148 SendRefreshFreeSizeTask();
149 }
150
RegisterAppStateCallback()151 void AppEventObserverMgr::RegisterAppStateCallback()
152 {
153 auto context = OHOS::AbilityRuntime::ApplicationContext::GetInstance();
154 if (context == nullptr) {
155 HILOG_WARN(LOG_CORE, "app context is null");
156 return;
157 }
158 appStateCallback_ = std::make_shared<AppStateCallback>();
159 context->RegisterAbilityLifecycleCallback(appStateCallback_);
160 HILOG_INFO(LOG_CORE, "succ to register application state callback");
161 }
162
~AppEventObserverMgr()163 AppEventObserverMgr::~AppEventObserverMgr()
164 {
165 UnregisterAppStateCallback();
166 queue_ = nullptr;
167 }
168
UnregisterAppStateCallback()169 void AppEventObserverMgr::UnregisterAppStateCallback()
170 {
171 if (appStateCallback_ == nullptr) {
172 return;
173 }
174
175 auto context = OHOS::AbilityRuntime::ApplicationContext::GetInstance();
176 if (context == nullptr) {
177 HILOG_WARN(LOG_CORE, "app context is null");
178 return;
179 }
180 context->UnregisterAbilityLifecycleCallback(appStateCallback_);
181 appStateCallback_ = nullptr;
182 HILOG_INFO(LOG_CORE, "succ to unregister application state callback");
183 }
184
SubmitTaskToFFRTQueue(std::function<void ()> && task,const std::string & taskName)185 void AppEventObserverMgr::SubmitTaskToFFRTQueue(std::function<void()>&& task, const std::string& taskName)
186 {
187 std::lock_guard<std::mutex> lock(queueMutex_);
188 if (queue_ == nullptr) {
189 HILOG_ERROR(LOG_CORE, "queue is null, failed to submit task=%{public}s", taskName.c_str());
190 return;
191 }
192 queue_->submit(task, ffrt::task_attr().name(taskName.c_str()));
193 }
194
GetSeqFromWatchers(const std::string & name,std::string & filters)195 int64_t AppEventObserverMgr::GetSeqFromWatchers(const std::string& name, std::string& filters)
196 {
197 std::lock_guard<std::mutex> lock(watcherMutex_);
198 for (auto it = watchers_.cbegin(); it != watchers_.cend(); ++it) {
199 if (it->second->GetName() == name) {
200 filters = it->second->GetFiltersStr();
201 return it->second->GetSeq();
202 }
203 }
204 return -1;
205 }
206
GetSeqFromProcessors(const std::string & name,int64_t hashCode)207 int64_t AppEventObserverMgr::GetSeqFromProcessors(const std::string& name, int64_t hashCode)
208 {
209 std::lock_guard<std::mutex> lock(processorMutex_);
210 for (auto it = processors_.cbegin(); it != processors_.cend(); ++it) {
211 if (it->second->GetName() == name && it->second->GenerateHashCode() == hashCode) {
212 return it->second->GetSeq();
213 }
214 }
215 return -1;
216 }
217
DeleteWatcher(int64_t observerSeq)218 void AppEventObserverMgr::DeleteWatcher(int64_t observerSeq)
219 {
220 std::lock_guard<std::mutex> lock(watcherMutex_);
221 watchers_.erase(observerSeq);
222 UnregisterOsEventListener();
223 }
224
DeleteProcessor(int64_t observerSeq)225 void AppEventObserverMgr::DeleteProcessor(int64_t observerSeq)
226 {
227 std::lock_guard<std::mutex> lock(processorMutex_);
228 processors_.erase(observerSeq);
229 }
230
IsExistInWatchers(int64_t observerSeq)231 bool AppEventObserverMgr::IsExistInWatchers(int64_t observerSeq)
232 {
233 std::lock_guard<std::mutex> lock(watcherMutex_);
234 return watchers_.find(observerSeq) != watchers_.cend();
235 }
236
IsExistInProcessors(int64_t observerSeq)237 bool AppEventObserverMgr::IsExistInProcessors(int64_t observerSeq)
238 {
239 std::lock_guard<std::mutex> lock(processorMutex_);
240 return processors_.find(observerSeq) != processors_.cend();
241 }
242
GetObservers()243 std::vector<std::shared_ptr<AppEventObserver>> AppEventObserverMgr::GetObservers()
244 {
245 std::vector<std::shared_ptr<AppEventObserver>> observers;
246 {
247 std::lock_guard<std::mutex> watcherLock(watcherMutex_);
248 for (auto it = watchers_.cbegin(); it != watchers_.cend(); ++it) {
249 observers.emplace_back(it->second);
250 }
251 }
252 {
253 std::lock_guard<std::mutex> processorLock(processorMutex_);
254 for (auto it = processors_.cbegin(); it != processors_.cend(); ++it) {
255 observers.emplace_back(it->second);
256 }
257 }
258 return observers;
259 }
260
InitWatchers()261 void AppEventObserverMgr::InitWatchers()
262 {
263 static std::once_flag onceFlag;
264 std::call_once(onceFlag, [&]() {
265 std::vector<Observer> observers;
266 if (AppEventStore::GetInstance().QueryWatchers(observers) != 0) {
267 HILOG_WARN(LOG_CORE, "failed to query observers from db");
268 return;
269 }
270 std::lock_guard<std::mutex> lock(watcherMutex_);
271 for (const auto& observer : observers) {
272 auto watcherPtr = std::make_shared<AppEventWatcher>(observer.name);
273 watcherPtr->SetSeq(observer.seq);
274 watcherPtr->SetFiltersStr(observer.filters);
275 watchers_[observer.seq] = watcherPtr;
276 }
277 HILOG_INFO(LOG_CORE, "init watchers");
278 });
279 }
280
InitWatcherFromCache(std::shared_ptr<AppEventWatcher> watcher,bool & isExist)281 void AppEventObserverMgr::InitWatcherFromCache(std::shared_ptr<AppEventWatcher> watcher, bool& isExist)
282 {
283 std::string name = watcher->GetName();
284 std::string filters = watcher->GetFiltersStr();
285 std::string historyFilters;
286 if (int64_t seq = GetSeqFromWatchers(name, historyFilters); seq > 0) {
287 isExist = true;
288 watcher->SetSeq(seq);
289 if (historyFilters != filters && AppEventStore::GetInstance().UpdateObserver(seq, filters) < 0) {
290 HILOG_ERROR(LOG_CORE, "failed to update watcher=%{public}s to db", name.c_str());
291 }
292 }
293 }
294
AddWatcher(std::shared_ptr<AppEventWatcher> watcher)295 int64_t AppEventObserverMgr::AddWatcher(std::shared_ptr<AppEventWatcher> watcher)
296 {
297 InitWatchers();
298 bool isExist = false;
299 InitWatcherFromCache(watcher, isExist);
300 std::string filters = watcher->GetFiltersStr();
301 int64_t observerSeq = InitObserverFromDb(watcher, filters);
302 if (observerSeq <= 0) {
303 return -1;
304 }
305 std::lock_guard<std::mutex> lock(watcherMutex_);
306 if (!InitWatcherFromListener(watcher, isExist)) {
307 return -1;
308 }
309 watchers_[observerSeq] = watcher;
310 HILOG_INFO(LOG_CORE, "register watcher=%{public}" PRId64 " successfully", observerSeq);
311 return observerSeq;
312 }
313
AddProcessorWithTimeLimited(const std::string & name,int64_t hashCode,std::shared_ptr<AppEventProcessorProxy> processor)314 int64_t AppEventObserverMgr::AddProcessorWithTimeLimited(const std::string& name, int64_t hashCode,
315 std::shared_ptr<AppEventProcessorProxy> processor)
316 {
317 if (isFirstAddProcessor_) {
318 isFirstAddProcessor_ = false;
319 auto syncPromise = std::make_shared<ffrt::promise<int64_t>>();
320 if (syncPromise == nullptr) {
321 HILOG_ERROR(LOG_CORE, "Failed to create syncPromise.");
322 return -1;
323 }
324 ffrt::future syncFuture = syncPromise->get_future();
325 ffrt::submit([this, syncPromise, processor, name, hashCode]() {
326 processor->SetSeq(AppEventStore::GetInstance().QueryObserverSeq(name, hashCode));
327 this->isDbInit_ = true;
328 syncPromise->set_value(InitObserverFromDb(processor, "", hashCode));
329 }, ffrt::task_attr()
330 .name("ADD_PROCESSOR_TASK")
331 .qos(static_cast<int>(ffrt_qos_user_initiated)));
332 ffrt::future_status wait = syncFuture.wait_for(std::chrono::milliseconds(TIMEOUT_LIMIT_FOR_ADDPROCESSOR));
333 if (wait != ffrt::future_status::ready) {
334 HILOG_WARN(LOG_CORE, "AddProcessor task execution timeout");
335 return -1;
336 }
337 return syncFuture.get();
338 }
339 int remainTime = TIMEOUT_LIMIT_FOR_ADDPROCESSOR;
340 while (!isDbInit_ && remainTime >= 0) {
341 remainTime -= CHECK_DB_INTERVAL;
342 std::this_thread::sleep_for(std::chrono::milliseconds(CHECK_DB_INTERVAL));
343 }
344 if (remainTime < 0) {
345 HILOG_WARN(LOG_CORE, "AddProcessor task execution timeout");
346 return -1;
347 }
348 processor->SetSeq(AppEventStore::GetInstance().QueryObserverSeq(name, hashCode));
349 return InitObserverFromDb(processor, "", hashCode);
350 }
351
AddProcessor(const std::string & name,const ReportConfig & config)352 int64_t AppEventObserverMgr::AddProcessor(const std::string& name, const ReportConfig& config)
353 {
354 if (name.empty()) {
355 HILOG_WARN(LOG_CORE, "processor name is empty");
356 return -1;
357 }
358
359 // build processor object
360 auto processor = moduleLoader_->CreateProcessorProxy(name);
361 if (processor == nullptr) {
362 HILOG_WARN(LOG_CORE, "processor is null");
363 return -1;
364 }
365 processor->SetReportConfig(config);
366
367 int64_t hashCode = processor->GenerateHashCode();
368 if (int64_t seq = GetSeqFromProcessors(name, hashCode); seq > 0) {
369 HILOG_INFO(LOG_CORE, "register processor=%{public}" PRId64 " exit", seq);
370 return seq;
371 }
372
373 int64_t observerSeq = AddProcessorWithTimeLimited(name, hashCode, processor);
374 if (observerSeq <= 0) {
375 return -1;
376 }
377 processor->ProcessStartup();
378 std::lock_guard<std::mutex> lock(processorMutex_);
379 processors_[observerSeq] = processor;
380 HILOG_INFO(LOG_CORE, "register processor=%{public}" PRId64 " successfully", observerSeq);
381 return observerSeq;
382 }
383
RemoveObserver(int64_t observerSeq)384 int AppEventObserverMgr::RemoveObserver(int64_t observerSeq)
385 {
386 if (!IsExistInWatchers(observerSeq) && !IsExistInProcessors(observerSeq)) {
387 HILOG_WARN(LOG_CORE, "observer seq=%{public}" PRId64 " is not exist", observerSeq);
388 return 0;
389 }
390 if (int ret = AppEventStore::GetInstance().DeleteObserver(observerSeq); ret < 0) {
391 HILOG_ERROR(LOG_CORE, "failed to unregister observer seq=%{public}" PRId64, observerSeq);
392 return ret;
393 }
394 DeleteProcessor(observerSeq);
395 DeleteWatcher(observerSeq);
396 HILOG_INFO(LOG_CORE, "unregister observer seq=%{public}" PRId64 " successfully", observerSeq);
397 return 0;
398 }
399
RemoveObserver(const std::string & observerName)400 int AppEventObserverMgr::RemoveObserver(const std::string& observerName)
401 {
402 std::vector<int64_t> deleteSeqs;
403 if (int ret = AppEventStore::GetInstance().QueryObserverSeqs(observerName, deleteSeqs); ret < 0) {
404 HILOG_ERROR(LOG_CORE, "failed to query observer=%{public}s seqs", observerName.c_str());
405 return ret;
406 }
407 int ret = 0;
408 for (auto deleteSeq : deleteSeqs) {
409 if (int tempRet = RemoveObserver(deleteSeq); tempRet < 0) {
410 HILOG_ERROR(LOG_CORE, "failed to unregister observer seq=%{public}" PRId64, deleteSeq);
411 ret = tempRet;
412 }
413 }
414 return ret;
415 }
416
Load(const std::string & moduleName)417 int AppEventObserverMgr::Load(const std::string& moduleName)
418 {
419 return moduleLoader_->Load(moduleName);
420 }
421
RegisterProcessor(const std::string & name,std::shared_ptr<AppEventProcessor> processor)422 int AppEventObserverMgr::RegisterProcessor(const std::string& name, std::shared_ptr<AppEventProcessor> processor)
423 {
424 return moduleLoader_->RegisterProcessor(name, processor);
425 }
426
UnregisterProcessor(const std::string & name)427 int AppEventObserverMgr::UnregisterProcessor(const std::string& name)
428 {
429 return moduleLoader_->UnregisterProcessor(name);
430 }
431
HandleEvents(std::vector<std::shared_ptr<AppEventPack>> & events)432 void AppEventObserverMgr::HandleEvents(std::vector<std::shared_ptr<AppEventPack>>& events)
433 {
434 InitWatchers();
435 auto observers = GetObservers();
436 if (observers.empty() || events.empty()) {
437 return;
438 }
439 HILOG_DEBUG(LOG_CORE, "start to handle events size=%{public}zu", events.size());
440 StoreEventsToDb(events);
441 StoreEventMappingToDb(events, observers);
442 bool isNeedSend = false;
443 for (const auto& observer : observers) {
444 // send events to observer, and then delete events not in event mapping
445 SendEventsToObserver(events, observer);
446 isNeedSend |= observer->HasTimeoutCondition();
447 }
448 // timeout condition > 0 and the current event row > 0, send timeout task.
449 // There can be only one timeout task.
450 std::lock_guard<std::mutex> lock(isTimeoutTaskExistMutex_);
451 if (isNeedSend && !isTimeoutTaskExist_) {
452 SendTimeoutTask();
453 isTimeoutTaskExist_ = true;
454 }
455 }
456
HandleTimeout()457 void AppEventObserverMgr::HandleTimeout()
458 {
459 auto observers = GetObservers();
460 bool isNeedSend = false;
461 for (const auto& observer : observers) {
462 observer->ProcessTimeout();
463 isNeedSend |= observer->HasTimeoutCondition();
464 }
465 if (isNeedSend) {
466 SendTimeoutTask();
467 } else {
468 // when the current event row <= 0, do not send timeout task.
469 std::lock_guard<std::mutex> lock(isTimeoutTaskExistMutex_);
470 isTimeoutTaskExist_ = false;
471 }
472 }
473
SendTimeoutTask()474 void AppEventObserverMgr::SendTimeoutTask()
475 {
476 ffrt::submit([this] {
477 HandleTimeout();
478 }, ffrt::task_attr().name("appevent_timeout").delay(TIMEOUT_INTERVAL_MILLI * MILLI_TO_MICRO));
479 }
480
SendRefreshFreeSizeTask()481 void AppEventObserverMgr::SendRefreshFreeSizeTask()
482 {
483 ffrt::submit([this] {
484 HiAppEventConfig::GetInstance().RefreshFreeSize();
485 SendRefreshFreeSizeTask();
486 }, ffrt::task_attr().name("appevent_refresh").delay(REFRESH_FREE_SIZE_INTERVAL * MILLI_TO_MICRO));
487 }
488
HandleBackground()489 void AppEventObserverMgr::HandleBackground()
490 {
491 HILOG_INFO(LOG_CORE, "start to handle background");
492 SubmitTaskToFFRTQueue([this] {
493 auto observers = GetObservers();
494 for (const auto& observer : observers) {
495 observer->ProcessBackground();
496 }
497 }, "app_background");
498 }
499
HandleClearUp()500 void AppEventObserverMgr::HandleClearUp()
501 {
502 HILOG_INFO(LOG_CORE, "start to handle clear up");
503 auto observers = GetObservers();
504 for (const auto& observer : observers) {
505 observer->ResetCurrCondition();
506 }
507 }
508
SetReportConfig(int64_t observerSeq,const ReportConfig & config)509 int AppEventObserverMgr::SetReportConfig(int64_t observerSeq, const ReportConfig& config)
510 {
511 std::lock_guard<std::mutex> lock(processorMutex_);
512 if (processors_.find(observerSeq) == processors_.cend()) {
513 HILOG_WARN(LOG_CORE, "failed to set config, seq=%{public}" PRId64, observerSeq);
514 return -1;
515 }
516 processors_[observerSeq]->SetReportConfig(config);
517 return 0;
518 }
519
GetReportConfig(int64_t observerSeq,ReportConfig & config)520 int AppEventObserverMgr::GetReportConfig(int64_t observerSeq, ReportConfig& config)
521 {
522 std::lock_guard<std::mutex> lock(processorMutex_);
523 if (processors_.find(observerSeq) == processors_.cend()) {
524 HILOG_WARN(LOG_CORE, "failed to get config, seq=%{public}" PRId64, observerSeq);
525 return -1;
526 }
527 config = processors_[observerSeq]->GetReportConfig();
528 return 0;
529 }
530
InitWatcherFromListener(std::shared_ptr<AppEventWatcher> watcher,bool isExist)531 bool AppEventObserverMgr::InitWatcherFromListener(std::shared_ptr<AppEventWatcher> watcher, bool isExist)
532 {
533 uint64_t mask = watcher->GetOsEventsMask();
534 if (mask == 0) {
535 return true;
536 }
537 if (listener_ == nullptr) {
538 listener_ = std::make_shared<OsEventListener>();
539 if (!listener_->StartListening()) {
540 return false;
541 }
542 }
543 if (!listener_->AddListenedEvents(mask)) {
544 return false;
545 }
546 if (isExist) {
547 std::vector<std::shared_ptr<AppEventPack>> events;
548 listener_->GetEvents(events);
549 StoreEventMappingToDb(events, {watcher});
550 SendEventsToObserver(events, watcher);
551 }
552 return true;
553 }
554
UnregisterOsEventListener()555 void AppEventObserverMgr::UnregisterOsEventListener()
556 {
557 if (listener_ == nullptr) {
558 return;
559 }
560 uint64_t mask = 0;
561 for (auto it = watchers_.cbegin(); it != watchers_.cend(); ++it) {
562 mask |= it->second->GetOsEventsMask();
563 }
564 if (mask > 0) {
565 listener_->SetListenedEvents(mask);
566 return;
567 }
568 listener_->RemoveOsEventDir();
569 listener_ = nullptr;
570 }
571 } // namespace HiviewDFX
572 } // namespace OHOS
573