• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 struct AppEventFilter {
29     /* Filtering events by event domain */
30     std::string domain;
31 
32     /* Filtering events by event names */
33     std::unordered_set<std::string> names;
34 
35     /* Filtering events by event types, stored in bits */
36     uint32_t types = 0;
37 
38     AppEventFilter(
39         const std::string& domain = "",
40         const std::unordered_set<std::string>& names = {},
41         uint32_t types = 0);
42     AppEventFilter(const std::string& domain, uint32_t types);
43 
44     bool IsValidEvent(std::shared_ptr<AppEventPack> event) const;
45 };
46 
47 class AppEventObserver {
48 public:
AppEventObserver(const std::string & name)49     AppEventObserver(const std::string& name) : name_(name) {}
50     virtual ~AppEventObserver() = default;
51     virtual void OnEvents(const std::vector<std::shared_ptr<AppEventPack>>& events) = 0;
52     virtual bool VerifyEvent(std::shared_ptr<AppEventPack> event);
53     virtual bool IsRealTimeEvent(std::shared_ptr<AppEventPack> event);
54     void ProcessEvent(std::shared_ptr<AppEventPack> event);
55     void ProcessTimeout();
56     void ProcessStartup();
57     void ProcessBackground();
58     bool HasOsDomain();
59 
60     std::string GetName();
61     int64_t GetSeq();
62     ReportConfig GetReportConfig();
63     void SetSeq(int64_t seq);
64     void SetCurrCondition(const TriggerCondition& triggerCond);
65     void SetReportConfig(const ReportConfig& reportConfig);
66 
67     // used to identify the observer with the same config
68     int64_t GenerateHashCode();
69 
70     // userd to reset the current status when condition is met or data is cleared.
71     void ResetCurrCondition();
72 
73 protected:
74     virtual void OnTrigger(const TriggerCondition& triggerCond);
75 
76 private:
77     int StoreEventToDb(std::shared_ptr<AppEventPack> event);
78     void QueryEventsFromDb(std::vector<std::shared_ptr<AppEventPack>>& events);
79     bool MeetProcessCondition();
80     bool MeetTimeoutCondition();
81     bool MeetStartupCondition();
82     bool MeetBackgroundCondition();
83 
84 protected:
85     std::vector<AppEventFilter> filters_;
86     ReportConfig reportConfig_;
87 
88 private:
89     std::string name_;
90     int64_t seq_ = 0; // observer sequence, used to uniquely identify an observer
91     TriggerCondition currCond_;
92 };
93 } // namespace HiAppEvent
94 } // namespace HiviewDFX
95 } // namespace OHOS
96 #endif // HIAPPEVENT_FRAMEWORKS_NATIVE_LIB_HIAPPEVENT_INCLUDE_APP_EVENT_OBSERVER_H
97