1 /* 2 * Copyright (c) 2021 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 16 #ifndef FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_STICKY_MANAGER_H 17 #define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_STICKY_MANAGER_H 18 19 #include "common_event_record.h" 20 #include "common_event_subscribe_info.h" 21 #include "singleton.h" 22 23 namespace OHOS { 24 namespace EventFwk { 25 class CommonEventStickyManager : public DelayedSingleton<CommonEventStickyManager> { 26 public: 27 using CommonEventRecordPtr = std::shared_ptr<CommonEventRecord>; 28 using CommonEventDataPtr = std::shared_ptr<CommonEventData>; 29 using CommonEventPublishInfoPtr = std::shared_ptr<CommonEventPublishInfo>; 30 using SubscribeInfoPtr = std::shared_ptr<CommonEventSubscribeInfo>; 31 32 /** 33 * Finds the sticky events. 34 * 35 * @param subscribeInfo Indicates the subscribe information. 36 * @param commonEventRecords Indicates the records of sticky common event. 37 * @return Returns result code. 38 */ 39 int FindStickyEvents(const SubscribeInfoPtr &subscribeInfo, std::vector<CommonEventRecordPtr> &commonEventRecords); 40 41 /** 42 * Gets the sticky event. 43 * 44 * @param event Indicates the event name. 45 * @param eventData Indicates the common event data. 46 * @return Returns true if successful; false otherwise. 47 */ 48 bool GetStickyCommonEvent(const std::string &event, CommonEventData &eventData); 49 50 /** 51 * Updates the sticky events. 52 * 53 * @param eventRecord Indicates the record of sticky common event. 54 * @return Returns result code. 55 */ 56 int UpdateStickyEvent(const CommonEventRecord &eventRecord); 57 58 /** 59 * Dumps the state of the sticky events. 60 * 61 * @param event Specifies the information for the common event. Set null string ("") if you want to dump all. 62 * @param userId Indicates the user ID. 63 * @param state Indicates the state of common event service. 64 */ 65 void DumpState(const std::string &event, const int32_t &userId, std::vector<std::string> &state); 66 67 /** 68 * Remove sticky common event. 69 * 70 * @param event Name of the common event. 71 * @param callerUid caller uid. 72 * @return Returns ERR_OK if success; otherwise failed. 73 */ 74 int32_t RemoveStickyCommonEvent(const std::string &event, uint32_t callerUid); 75 76 private: 77 void FindStickyEventsLocked( 78 const std::vector<std::string> &events, std::vector<CommonEventRecordPtr> &commonEventRecords); 79 80 bool GetStickyCommonEventLocked(const std::string &event, CommonEventData &eventData); 81 82 int UpdateStickyEventLocked(const std::string &event, const CommonEventRecordPtr &record); 83 84 void GetStickyCommonEventRecords( 85 const std::string &event, const int32_t &userId, std::vector<CommonEventRecordPtr> &records); 86 87 private: 88 std::mutex mutex_; 89 std::map<std::string, CommonEventRecordPtr> commonEventRecords_; 90 }; 91 } // namespace EventFwk 92 } // namespace OHOS 93 94 #endif // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_STICKY_MANAGER_H