• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "json/json.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     Json::Value ToJsonValue() const;
51 };
52 
53 class AppEventObserver {
54 public:
AppEventObserver(const std::string & name)55     AppEventObserver(const std::string& name) : name_(name) {}
56     virtual ~AppEventObserver() = default;
OnEvents(const std::vector<std::shared_ptr<AppEventPack>> & events)57     virtual void OnEvents(const std::vector<std::shared_ptr<AppEventPack>>& events) {}
58     virtual bool VerifyEvent(std::shared_ptr<AppEventPack> event);
59     virtual bool IsRealTimeEvent(std::shared_ptr<AppEventPack> event);
60     void ProcessEvent(std::shared_ptr<AppEventPack> event);
61     void ProcessTimeout();
62     void ProcessStartup();
63     void ProcessBackground();
64     bool HasTimeoutCondition();
65 
66     std::string GetName();
67     int64_t GetSeq();
68     ReportConfig GetReportConfig();
69     void SetSeq(int64_t seq);
70     void SetCurrCondition(const TriggerCondition& triggerCond);
71     void SetReportConfig(const ReportConfig& reportConfig);
72 
73     // used to identify the observer with the same config
74     int64_t GenerateHashCode();
75 
76     // used to reset the current status when condition is met or data is cleared.
77     void ResetCurrCondition();
78 
79     // used to match os events.
80     uint64_t GetOsEventsMask();
81     void SetFilters(const std::string& jsonFiltersStr);
82     std::string GetFiltersStr();
83 
84 protected:
85     virtual void OnTrigger(const TriggerCondition& triggerCond);
86 
87 private:
88     void QueryEventsFromDb(std::vector<std::shared_ptr<AppEventPack>>& events);
89     bool MeetProcessCondition();
90     bool MeetTimeoutCondition();
91     bool MeetStartupCondition();
92     bool MeetBackgroundCondition();
93 
94 protected:
95     std::vector<AppEventFilter> filters_;
96     ReportConfig reportConfig_;
97 
98 private:
99     std::string name_;
100     int64_t seq_ = 0; // observer sequence, used to uniquely identify an observer
101     TriggerCondition currCond_;
102 };
103 } // namespace HiAppEvent
104 } // namespace HiviewDFX
105 } // namespace OHOS
106 #endif // HIAPPEVENT_FRAMEWORKS_NATIVE_LIB_HIAPPEVENT_INCLUDE_APP_EVENT_OBSERVER_H
107