/* * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_SUBSCRIBER_MANAGER_H #define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_SUBSCRIBER_MANAGER_H #include "common_event_constant.h" #include "common_event_record.h" #include "common_event_subscribe_info.h" #include "iremote_object.h" #include "singleton.h" namespace OHOS { namespace EventFwk { struct EventSubscriberRecord { std::shared_ptr eventSubscribeInfo; sptr commonEventListener; EventRecordInfo eventRecordInfo; struct tm recordTime {0}; bool isFreeze; int64_t freezeTime; EventSubscriberRecord() : eventSubscribeInfo(nullptr), commonEventListener(nullptr), isFreeze(false), freezeTime(0) {} }; struct FrozenEventRecord { std::shared_ptr subscriberRecordPtr; std::vector> eventRecordPtrs; FrozenEventRecord() : subscriberRecordPtr(nullptr) {} }; inline bool operator<(const std::shared_ptr &a, const std::shared_ptr &b) { if (a == nullptr || a->eventSubscribeInfo == nullptr) { return true; } if (b == nullptr || b->eventSubscribeInfo == nullptr) { return false; } return a->eventSubscribeInfo->GetPriority() > b->eventSubscribeInfo->GetPriority(); } using SubscriberRecordPtr = std::shared_ptr; using SubscribeInfoPtr = std::shared_ptr; using EventRecordPtr = std::shared_ptr; using FrozenRecords = std::map>; class CommonEventSubscriberManager : public DelayedSingleton { public: CommonEventSubscriberManager(); virtual ~CommonEventSubscriberManager() override; /** * Inserts a specific subscriber. * * @param eventSubscribeInfo Indicates the subscribe information. * @param commonEventListener Indicates the subscriber object. * @param recordTime Indicates the time of record. * @param eventRecordInfo Indicates the information of event record. * @return Returns the subscribe record. */ std::shared_ptr InsertSubscriber(const SubscribeInfoPtr &eventSubscribeInfo, const sptr &commonEventListener, const struct tm &recordTime, const EventRecordInfo &eventRecordInfo); /** * Removes subscriber. * * @param commonEventListener Indicates the subscriber object. * @return Returns the result code. */ int RemoveSubscriber(const sptr &commonEventListener); /** * Gets subscriber records. * * @param eventRecord Indicates the event record. * @return Returns the subscriber records. */ std::vector GetSubscriberRecords(const CommonEventRecord &eventRecord); /** * @brief Get the subscribe record by subscriber object. * * @param commonEventListener Indicates the subscriber object. * @return std::shared_ptr */ std::shared_ptr GetSubscriberRecord(const sptr &commonEventListener); /** * Updates freeze information. * * @param uid Indicates the uid of the application. * @param freezeState Indicates the freeze state. * @param freezeTime Indicates the freeze time. */ void UpdateFreezeInfo(const uid_t &uid, const bool &freezeState, const int64_t &freezeTime = 0); /** * Updates freeze information of all applications. * * @param freezeState Indicates the freeze state. * @param freezeTime Indicates the freeze time. */ void UpdateAllFreezeInfos(const bool &freezeState, const int64_t &freezeTime = 0); /** * Inserts freeze events. * * @param eventListener Indicates the subscriber object. * @param eventRecord Indicates the event record. */ void InsertFrozenEvents(const SubscriberRecordPtr &eventListener, const CommonEventRecord &eventRecord); /** * Gets the frozen events. * * @param uid Indicates the uid of the application. * @return Returns the frozen events. */ FrozenRecords GetFrozenEvents(const uid_t &uid); /** * Gets all frozen events. * * @return Returns all frozen events. */ std::map GetAllFrozenEvents(); /** * Dumps detailed information for specific subscriber record info. * * @param title Indicates the log tag. * @param record Indicates the subscriber record. * @param format Indicates the log format. * @param dumpInfo Indicates the output information. */ void DumpDetailed( const std::string &title, const SubscriberRecordPtr &record, const std::string format, std::string &dumpInfo); /** * Dumps state information. * * @param event Specifies the information for the common event. Set null string ("") if you want to dump all. * @param userId Indicates the user ID. * @param state Indicates the output information. */ void DumpState(const std::string &event, const int32_t &userId, std::vector &state); private: bool InsertSubscriberRecordLocked(const std::vector &events, const SubscriberRecordPtr &record); int RemoveSubscriberRecordLocked(const sptr &commonEventListener); bool CheckSubscriberByUserId(const int32_t &subscriberUserId, const bool &isSystemApp, const int32_t &userId); void GetSubscriberRecordsByWantLocked(const CommonEventRecord &eventRecord, std::vector &records); void GetSubscriberRecordsByEvent( const std::string &event, const int32_t &userId, std::vector &records); void RemoveFrozenEventsBySubscriber(const SubscriberRecordPtr &subscriberRecord); void RemoveFrozenEvents(const uid_t &uid); void SendSubscriberExceedMaximumHiSysEvent(int32_t userId, const std::string &eventName, uint32_t subscriberNum); private: std::mutex mutex_; sptr death_; std::map> eventSubscribers_; std::vector subscribers_; std::map frozenEvents_; const time_t FREEZE_EVENT_TIMEOUT = 30; // How long we keep records. Unit: second }; } // namespace EventFwk } // namespace OHOS #endif // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_SUBSCRIBER_MANAGER_H