• 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 SYSTEM_ABILITY_MANAGER_COMMON_EVENT_COLLECT_H
16 #define SYSTEM_ABILITY_MANAGER_COMMON_EVENT_COLLECT_H
17 
18 #include <memory>
19 #include <thread>
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 std::vector<OnDemandEvent>& events) 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     void StartReclaimIpcThreadWork(const EventFwk::CommonEventData& data);
62     void StartMonitorThread();
63     void StopMonitorThread();
64 
65 private:
66     int64_t GenerateExtraDataIdLocked();
67     std::vector<std::string> AddCommonEventName(const std::vector<OnDemandEvent>& events);
68     void AddSkillsEvent(EventFwk::MatchingSkills& skill);
69     void CleanFailedEventLocked(const std::vector<std::string>& eventNames);
70     void SendKernalReclaimIpcThread();
71     bool GetCpuTimes(const char* file, uint64_t& total, uint64_t& idle);
72     float GetCpuUsage(const char* file, uint32_t interval);
73     void MonitorCpuUsageThread();
74     samgr::mutex commomEventLock_;
75     samgr::mutex commonEventSubscriberLock_;
76     sptr<IRemoteObject::DeathRecipient> commonEventDeath_;
77     std::set<std::string> commonEventNames_;
78     std::shared_ptr<CommonHandler> workHandler_;
79     std::shared_ptr<CommonHandler> unsubHandler_;
80     std::shared_ptr<EventFwk::CommonEventSubscriber> commonEventSubscriber_ = nullptr;
81     samgr::mutex commonEventStateLock_;
82     std::set<std::string> commonEventWhitelist;
83     std::map<std::string, std::map<std::string, std::string>> commonEventConditionExtraData_;
84     std::map<std::string, std::string> commonEventConditionValue_;
85     samgr::mutex extraDataLock_;
86     int64_t extraDataId_ = 0;
87     std::map<int64_t, OnDemandReasonExtraData> extraDatas_;
88     samgr::mutex saExtraDataIdLock_;
89     std::map<int32_t, std::list<int64_t>> saExtraDataIdMap_;
90     std::atomic<bool> isAwakeNotified_ {false};
91     std::atomic<bool> isTriggerTaskStart_ {false};
92     std::atomic<bool> isCancel_{false};
93     std::atomic<bool> keepRunning_ {false};
94     std::thread monitorThread_;
95 };
96 
97 class CommonEventListener : public SystemAbilityStatusChangeStub {
98 public:
99     CommonEventListener(const sptr<CommonEventCollect>& commonEventCollect);
100     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
101     void OnRemoveSystemAbility(int32_t systemAblityId, const std::string& deviceId) override;
102 private:
103     sptr<CommonEventCollect> commonEventCollect_;
104 };
105 class CommonHandler {
106     public:
CommonHandler(const sptr<CommonEventCollect> & collect)107         CommonHandler(const sptr<CommonEventCollect>& collect) :commonCollect_(collect)
108         {
109             handler_ = std::make_shared<FFRTHandler>("CommonHandler");
110         }
111         ~CommonHandler() = default;
112         void ProcessEvent(uint32_t eventId, int64_t extraDataId);
113         bool SendEvent(uint32_t eventId);
114         bool SendEvent(uint32_t eventId, int64_t extraDataId, uint64_t delayTime);
115         void CleanFfrt();
116         void SetFfrt();
117         bool PostTask(std::function<void()> func, uint64_t delayTime);
118         bool PostTask(std::function<void()> func, const std::string& name, uint64_t delayTime);
119         void RemoveTask(const std::string& name);
120         void DelTask(const std::string& name);
121 
122     private:
123         wptr<CommonEventCollect> commonCollect_;
124         std::shared_ptr<FFRTHandler> handler_;
125 };
126 
127 class CommonEventSubscriber : public EventFwk::CommonEventSubscriber {
128 public:
129     CommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscribeInfo,
130         const sptr<CommonEventCollect>& collect);
131     ~CommonEventSubscriber() override = default;
132     void OnReceiveEvent(const EventFwk::CommonEventData& data) override;
133 private:
134     wptr<CommonEventCollect> collect_;
135 };
136 } // namespace OHOS
137 #endif // SYSTEM_ABILITY_MANAGER_COMMON_EVENT_COLLECT_H