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, 40 std::vector<CommonEventRecordPtr> &commonEventRecords); 41 42 /** 43 * Gets the sticky event. 44 * 45 * @param event Indicates the event name. 46 * @param eventData Indicates the common event data. 47 * @return Returns true if successful; false otherwise. 48 */ 49 bool GetStickyCommonEvent(const std::string &event, CommonEventData &eventData); 50 51 /** 52 * Updates the sticky events. 53 * 54 * @param eventRecord Indicates the record of sticky common event. 55 * @return Returns result code. 56 */ 57 int UpdateStickyEvent(const CommonEventRecord &eventRecord); 58 59 /** 60 * Dumps the state of the sticky events. 61 * 62 * @param event Specifies the information for the common event. Set null string ("") if you want to dump all. 63 * @param userId Indicates the user ID. 64 * @param state Indicates the state of common event service. 65 */ 66 void DumpState(const std::string &event, const int32_t &userId, std::vector<std::string> &state); 67 68 /** 69 * Remove sticky common event. 70 * 71 * @param event Name of the common event. 72 * @param callerUid caller uid. 73 * @return Returns ERR_OK if success; otherwise failed. 74 */ 75 int32_t RemoveStickyCommonEvent(const std::string &event, uint32_t callerUid); 76 77 private: 78 void FindStickyEventsLocked( 79 const std::vector<std::string> &events, std::vector<CommonEventRecordPtr> &commonEventRecords); 80 81 bool GetStickyCommonEventLocked(const std::string &event, CommonEventData &eventData); 82 83 int UpdateStickyEventLocked(const std::string &event, const CommonEventRecordPtr &record); 84 85 void GetStickyCommonEventRecords( 86 const std::string &event, const int32_t &userId, std::vector<CommonEventRecordPtr> &records); 87 88 private: 89 std::mutex mutex_; 90 std::map<std::string, CommonEventRecordPtr> commonEventRecords_; 91 }; 92 } // namespace EventFwk 93 } // namespace OHOS 94 95 #endif // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_STICKY_MANAGER_H