1 /* 2 * Copyright (c) 2022-2024 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 "ffrt.h" 25 #include "module_loader.h" 26 #include "nocopyable.h" 27 28 namespace OHOS { 29 namespace HiviewDFX { 30 class AppEventHandler; 31 class OsEventListener; 32 namespace HiAppEvent { 33 class AppStateCallback; 34 } 35 using HiAppEvent::AppEventObserver; 36 using HiAppEvent::AppStateCallback; 37 using HiAppEvent::AppEventProcessor; 38 using HiAppEvent::ModuleLoader; 39 using HiAppEvent::ReportConfig; 40 41 class AppEventObserverMgr : public NoCopyable { 42 public: 43 static AppEventObserverMgr& GetInstance(); 44 45 void CreateEventHandler(); 46 void DestroyEventHandler(); 47 int64_t RegisterObserver(std::shared_ptr<AppEventObserver> observer); 48 int64_t RegisterObserver(const std::string& observerName, const ReportConfig& config = {}); 49 int UnregisterObserver(int64_t observerSeq); 50 int UnregisterObserver(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 59 int SetReportConfig(int64_t observerSeq, const ReportConfig& config); 60 int GetReportConfig(int64_t observerSeq, ReportConfig& config); 61 62 private: 63 AppEventObserverMgr(); 64 ~AppEventObserverMgr(); 65 void SendEventToHandler(); 66 void RegisterAppStateCallback(); 67 void UnregisterAppStateCallback(); 68 bool InitObserverFromListener(std::shared_ptr<AppEventObserver> observer, bool sendFlag); 69 void UnregisterOsEventListener(); 70 71 private: 72 std::unique_ptr<ModuleLoader> moduleLoader_; // moduleLoader_ must declared before observers_, or lead to crash 73 std::unordered_map<int64_t, std::shared_ptr<AppEventObserver>> observers_; 74 std::shared_ptr<AppEventHandler> handler_; 75 std::shared_ptr<AppStateCallback> appStateCallback_; 76 ffrt::mutex observerMutex_; 77 std::shared_ptr<OsEventListener> listener_; 78 bool hasHandleTimeout_ = false; 79 ffrt::mutex handlerMutex_; 80 }; 81 } // namespace HiviewDFX 82 } // namespace OHOS 83 #endif // HIAPPEVENT_FRAMEWORKS_NATIVE_LIB_HIAPPEVENT_OBSERVER_APP_EVENT_OBSERVER_MGR_H 84