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