1 /* 2 * Copyright (c) 2023-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_INCLUDE_APP_EVENT_OBSERVER_H 16 #define HIAPPEVENT_FRAMEWORKS_NATIVE_LIB_HIAPPEVENT_INCLUDE_APP_EVENT_OBSERVER_H 17 18 #include <mutex> 19 #include <string> 20 #include <unordered_map> 21 #include <unordered_set> 22 #include <vector> 23 24 #include "base_type.h" 25 26 namespace OHOS { 27 namespace HiviewDFX { 28 namespace HiAppEvent { 29 constexpr int TIMEOUT_STEP = 30; // step of time is 30s 30 31 struct AppEventFilter { 32 /* Filtering events by event domain */ 33 std::string domain; 34 35 /* Filtering events by event names */ 36 std::unordered_set<std::string> names; 37 38 /* Filtering events by event types, stored in bits */ 39 uint32_t types = 0; 40 41 AppEventFilter( 42 const std::string& domain = "", 43 const std::unordered_set<std::string>& names = {}, 44 uint32_t types = 0); 45 AppEventFilter(const std::string& domain, uint32_t types); 46 47 bool IsValidEvent(std::shared_ptr<AppEventPack> event) const; 48 bool IsValidEvent(const std::string& eventDomain, const std::string& eventName, int eventType) const; 49 uint64_t GetOsEventsMask() const; 50 }; 51 52 class AppEventObserver { 53 public: AppEventObserver(const std::string & name)54 AppEventObserver(const std::string& name) : name_(name) {} AppEventObserver(const std::string & name,const std::vector<AppEventFilter> & filters,TriggerCondition cond)55 AppEventObserver( 56 const std::string& name, 57 const std::vector<AppEventFilter>& filters, 58 TriggerCondition cond) : name_(name), filters_(filters), triggerCond_(cond) {} 59 virtual ~AppEventObserver() = default; OnEvents(const std::vector<std::shared_ptr<AppEventPack>> & events)60 virtual void OnEvents(const std::vector<std::shared_ptr<AppEventPack>>& events) {} 61 virtual bool VerifyEvent(std::shared_ptr<AppEventPack> event); IsRealTimeEvent(std::shared_ptr<AppEventPack> event)62 virtual bool IsRealTimeEvent(std::shared_ptr<AppEventPack> event) { return false; } OnTrigger(const TriggerCondition & triggerCond)63 virtual void OnTrigger(const TriggerCondition& triggerCond) {} 64 void ProcessEvent(std::shared_ptr<AppEventPack> event); 65 void ProcessTimeout(); 66 void ProcessStartup(); 67 void ProcessBackground(); 68 bool HasTimeoutCondition(); 69 70 std::string GetName(); 71 int64_t GetSeq(); 72 void SetSeq(int64_t seq); 73 void SetCurrCondition(const TriggerCondition& triggerCond); 74 // used to reset the current status when condition is met or data is cleared. 75 void ResetCurrCondition(); 76 void SetTriggerCond(const TriggerCondition& triggerCond); 77 std::vector<AppEventFilter> GetFilters(); 78 void SetFilters(const std::vector<AppEventFilter>& filters); 79 void AddFilter(const AppEventFilter& filter); 80 81 private: 82 std::string name_; 83 int64_t seq_ = 0; // observer sequence, used to uniquely identify an observer 84 std::vector<AppEventFilter> filters_; 85 TriggerCondition triggerCond_; 86 TriggerCondition currCond_; 87 std::mutex mutex_; 88 std::mutex condMutex_; 89 }; 90 } // namespace HiAppEvent 91 } // namespace HiviewDFX 92 } // namespace OHOS 93 #endif // HIAPPEVENT_FRAMEWORKS_NATIVE_LIB_HIAPPEVENT_INCLUDE_APP_EVENT_OBSERVER_H 94