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