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