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 SYSTEM_ABILITY_MANAGER_COMMON_EVENT_COLLECT_H
16 #define SYSTEM_ABILITY_MANAGER_COMMON_EVENT_COLLECT_H
17 
18 #include <memory>
19 #include <mutex>
20 
21 #include "common_event_subscriber.h"
22 #include "ffrt_handler.h"
23 #include "icollect_plugin.h"
24 #include "iremote_object.h"
25 #include "system_ability_status_change_stub.h"
26 
27 namespace OHOS {
28 class CommonHandler;
29 class CommonEventCollect : public ICollectPlugin {
30 public:
31     explicit CommonEventCollect(const sptr<IReport>& report);
32     ~CommonEventCollect() = default;
33 
34     void CleanFfrt() override;
35     void SetFfrt() override;
36     int32_t OnStart() override;
37     int32_t OnStop() override;
38     void SaveAction(const std::string& action);
39     bool CheckCondition(const OnDemandCondition& condition) override;
40     int32_t AddCollectEvent(const OnDemandEvent& event) override;
41     int32_t RemoveUnusedEvent(const OnDemandEvent& event) override;
42     void Init(const std::list<SaProfile>& saProfiles) override;
43     int64_t SaveOnDemandReasonExtraData(const EventFwk::CommonEventData& data);
44     void RemoveOnDemandReasonExtraData(int64_t extraDataId);
45     bool GetOnDemandReasonExtraData(int64_t extraDataId, OnDemandReasonExtraData& extraData) override;
46     bool CreateCommonEventSubscriber();
47     bool CreateCommonEventSubscriberLocked();
48     bool SendEvent(uint32_t eventId);
49     std::string GetParamFromWant(const std::string& key, const AAFwk::Want& data);
50     void InitCommonEventState(const OnDemandEvent& evnet);
51     bool CheckExtraMessage(int64_t extraDataId, const OnDemandEvent& profileEvent) override;
52     void SaveOnDemandConditionExtraData(const EventFwk::CommonEventData& data);
53     void SaveSaExtraDataId(int32_t saId, int64_t extraDataId) override;
54     void RemoveSaExtraDataId(int64_t extraDataId);
55     void ClearSaExtraDataId(int32_t saId) override;
56     void SaveCacheCommonEventSaExtraId(const OnDemandEvent& event,
57         const std::list<SaControlInfo>& saControlList) override;
58     int GetSaExtraDataIdList(int32_t saId, std::vector<int64_t>& extraDataIdList,
59         const std::string& eventName = "") override;
60     void RemoveWhiteCommonEvent() override;
61 private:
62     int64_t GenerateExtraDataIdLocked();
63     bool AddCommonEventName(const std::string& eventName);
64     void AddSkillsEvent(EventFwk::MatchingSkills& skill);
65     void CleanFailedEventLocked(const std::string& eventName);
66     std::mutex commomEventLock_;
67     std::mutex commonEventSubscriberLock_;
68     sptr<IRemoteObject::DeathRecipient> commonEventDeath_;
69     std::set<std::string> commonEventNames_;
70     std::shared_ptr<CommonHandler> workHandler_;
71     std::shared_ptr<CommonHandler> unsubHandler_;
72     std::shared_ptr<EventFwk::CommonEventSubscriber> commonEventSubscriber_ = nullptr;
73     std::mutex commonEventStateLock_;
74     std::set<std::string> commonEventWhitelist;
75     std::map<std::string, std::map<std::string, std::string>> commonEventConditionExtraData_;
76     std::map<std::string, std::string> commonEventConditionValue_;
77     std::mutex extraDataLock_;
78     int64_t extraDataId_ = 0;
79     std::map<int64_t, OnDemandReasonExtraData> extraDatas_;
80     std::map<std::string, std::set<std::string>> extraDataKey_;
81     std::mutex saExtraDataIdLock_;
82     std::map<int32_t, std::list<int64_t>> saExtraDataIdMap_;
83 };
84 
85 class CommonEventListener : public SystemAbilityStatusChangeStub {
86 public:
87     CommonEventListener(const sptr<CommonEventCollect>& commonEventCollect);
88     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
89     void OnRemoveSystemAbility(int32_t systemAblityId, const std::string& deviceId) override;
90 private:
91     sptr<CommonEventCollect> commonEventCollect_;
92 };
93 class CommonHandler {
94     public:
CommonHandler(const sptr<CommonEventCollect> & collect)95         CommonHandler(const sptr<CommonEventCollect>& collect) :commonCollect_(collect)
96         {
97             handler_ = std::make_shared<FFRTHandler>("CommonHandler");
98         }
99         ~CommonHandler() = default;
100         void ProcessEvent(uint32_t eventId, int64_t extraDataId);
101         bool SendEvent(uint32_t eventId);
102         bool SendEvent(uint32_t eventId, int64_t extraDataId, uint64_t delayTime);
103         void CleanFfrt();
104         void SetFfrt();
105         bool PostTask(std::function<void()> func, uint64_t delayTime);
106 
107     private:
108         wptr<CommonEventCollect> commonCollect_;
109         std::shared_ptr<FFRTHandler> handler_;
110 };
111 
112 class CommonEventSubscriber : public EventFwk::CommonEventSubscriber {
113 public:
114     CommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscribeInfo,
115         const sptr<CommonEventCollect>& collect);
116     ~CommonEventSubscriber() override = default;
117     void OnReceiveEvent(const EventFwk::CommonEventData& data) override;
118 private:
119     wptr<CommonEventCollect> collect_;
120 };
121 } // namespace OHOS
122 #endif // SYSTEM_ABILITY_MANAGER_COMMON_EVENT_COLLECT_H