• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef HIAPPEVENT_FRAMEWORKS_NATIVE_LIB_HIAPPEVENT_OBSERVER_APP_EVENT_OBSERVER_MGR_H
16 #define HIAPPEVENT_FRAMEWORKS_NATIVE_LIB_HIAPPEVENT_OBSERVER_APP_EVENT_OBSERVER_MGR_H
17 
18 #include <memory>
19 #include <mutex>
20 #include <unordered_map>
21 
22 #include "app_event_observer.h"
23 #include "app_event_processor.h"
24 #include "app_event_processor_proxy.h"
25 #include "app_event_watcher.h"
26 #include "ffrt.h"
27 #include "module_loader.h"
28 #include "nocopyable.h"
29 
30 namespace OHOS {
31 namespace HiviewDFX {
32 class OsEventListener;
33 namespace HiAppEvent {
34 class AppStateCallback;
35 }
36 using HiAppEvent::AppEventObserver;
37 using HiAppEvent::AppStateCallback;
38 using HiAppEvent::AppEventProcessor;
39 using HiAppEvent::ModuleLoader;
40 using HiAppEvent::ReportConfig;
41 using HiAppEvent::AppEventProcessorProxy;
42 
43 class AppEventObserverMgr : public NoCopyable {
44 public:
45     static AppEventObserverMgr& GetInstance();
46 
47     int64_t AddWatcher(std::shared_ptr<AppEventWatcher> watcher);
48     int64_t AddProcessor(const std::string& name, const ReportConfig& config = {});
49     int RemoveObserver(int64_t observerSeq);
50     int RemoveObserver(const std::string& observerName);
51     int Load(const std::string& moduleName);
52     int RegisterProcessor(const std::string& name, std::shared_ptr<AppEventProcessor> processor);
53     int UnregisterProcessor(const std::string& name);
54     void HandleEvents(std::vector<std::shared_ptr<AppEventPack>>& events);
55     void HandleTimeout();
56     void HandleBackground();
57     void HandleClearUp();
58     int SetReportConfig(int64_t observerSeq, const ReportConfig& config);
59     int GetReportConfig(int64_t observerSeq, ReportConfig& config);
60     void SubmitTaskToFFRTQueue(std::function<void()>&& task, const std::string& taskName);
61 
62 private:
63     AppEventObserverMgr();
64     ~AppEventObserverMgr();
65     int64_t AddProcessorWithTimeLimited(const std::string& name, int64_t hashCode,
66         std::shared_ptr<AppEventProcessorProxy> processor);
67     void SendTimeoutTask();
68     void SendRefreshFreeSizeTask();
69     void RegisterAppStateCallback();
70     void UnregisterAppStateCallback();
71     bool InitWatcherFromListener(std::shared_ptr<AppEventWatcher> watcher, bool sendFlag);
72     void UnregisterOsEventListener();
73     void InitWatchers();
74     void InitWatcherFromCache(std::shared_ptr<AppEventWatcher> watcher, bool& isExist);
75     int64_t GetSeqFromWatchers(const std::string& name, std::string& filters);
76     int64_t GetSeqFromProcessors(const std::string& name, int64_t hashCode);
77     std::vector<std::shared_ptr<AppEventObserver>> GetObservers();
78     void DeleteWatcher(int64_t observerSeq);
79     void DeleteProcessor(int64_t observerSeq);
80     bool IsExistInWatchers(int64_t observerSeq);
81     bool IsExistInProcessors(int64_t observerSeq);
82 
83 private:
84     std::unique_ptr<ModuleLoader> moduleLoader_; // moduleLoader_ must declared before observers_, or lead to crash
85     std::unordered_map<int64_t, std::shared_ptr<AppEventWatcher>> watchers_;
86     std::unordered_map<int64_t, std::shared_ptr<AppEventProcessorProxy>> processors_;
87     std::mutex watcherMutex_;
88     std::mutex processorMutex_;
89     std::shared_ptr<ffrt::queue> queue_ = nullptr;
90     std::mutex queueMutex_;
91     std::shared_ptr<AppStateCallback> appStateCallback_;
92     std::shared_ptr<OsEventListener> listener_ = nullptr;
93     bool isTimeoutTaskExist_ = false;
94     std::mutex isTimeoutTaskExistMutex_;
95     std::atomic<bool> isFirstAddProcessor_ = true;
96     std::atomic<int> isDbInit_ = false;
97 };
98 } // namespace HiviewDFX
99 } // namespace OHOS
100 #endif // HIAPPEVENT_FRAMEWORKS_NATIVE_LIB_HIAPPEVENT_OBSERVER_APP_EVENT_OBSERVER_MGR_H
101