• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #ifndef FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_INNER_COMMON_EVENT_MANAGER_H
17 #define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_INNER_COMMON_EVENT_MANAGER_H
18 
19 #include "access_token_helper.h"
20 #include "common_event_control_manager.h"
21 #include "icommon_event.h"
22 #include "static_subscriber_manager.h"
23 
24 namespace OHOS {
25 namespace EventFwk {
26 struct EventComeFrom {
27     bool isSubsystem = false;
28     bool isSystemApp = false;
29     bool isProxy = false;
30     bool isCemShell = false;
31 };
32 
33 class InnerCommonEventManager {
34 public:
35     InnerCommonEventManager();
36 
~InnerCommonEventManager()37     virtual ~InnerCommonEventManager() {};
38 
39     /**
40      * Publishes a common event.
41      *
42      * @param data Indicates the common event data.
43      * @param publishInfo Indicates the publish info.
44      * @param commonEventListener Indicates the common event subscriber.
45      * @param recordTime Indicates the time of record.
46      * @param pid Indicates the pid of application.
47      * @param uid Indicates the uid of application.
48      * @param callerToken Indicates the token of caller.
49      * @param userId Indicates the user ID.
50      * @param bundleName Indicates the name of bundle.
51      * @param service Indicates the common event service.
52      * @return Returns true if successful; false otherwise.
53      */
54     bool PublishCommonEvent(const CommonEventData &data, const CommonEventPublishInfo &publishinfo,
55         const sptr<IRemoteObject> &commonEventListener, const struct tm &recordTime, const pid_t &pid, const uid_t &uid,
56         const Security::AccessToken::AccessTokenID &callerToken, const int32_t &userId, const std::string &bundleName,
57         const sptr<IRemoteObject> &service = nullptr);
58 
59     /**
60      * Subscribes to common events.
61      *
62      * @param subscribeInfo Indicates the subscribe info.
63      * @param commonEventListener Indicates the common event subscriber.
64      * @param recordTime Indicates the time of record.
65      * @param pid Indicates the pid of application.
66      * @param uid Indicates the uid of application.
67      * @param callerToken Indicates the token of caller.
68      * @param bundleName Indicates the name of bundle.
69      * @return Returns true if successful; false otherwise.
70      */
71     bool SubscribeCommonEvent(const CommonEventSubscribeInfo &subscribeInfo,
72         const sptr<IRemoteObject> &commonEventListener, const struct tm &recordTime, const pid_t &pid, const uid_t &uid,
73         const Security::AccessToken::AccessTokenID &callerToken, const std::string &bundleName);
74 
75     /**
76      * Unsubscribes from common events.
77      *
78      * @param commonEventListener Indicates the common event subscriber.
79      * @return Returns true if successful; false otherwise.
80      */
81     bool UnsubscribeCommonEvent(const sptr<IRemoteObject> &commonEventListener);
82 
83     /**
84      * Gets the current sticky common event
85      *
86      * @param event Indicates the common event.
87      * @param eventData Indicates the common event data.
88      * @return Returns true if successful; false otherwise.
89      */
90     bool GetStickyCommonEvent(const std::string &event, CommonEventData &eventData);
91 
92     /**
93      * Dumps state of common event service.
94      *
95      * @param dumpType Indicates the dump type.
96      * @param event Specifies the information for the common event. Set null string ("") if you want to dump all.
97      * @param userId Indicates the user ID.
98      * @param state Indicates the state of common event service.
99      */
100     void DumpState(const uint8_t &dumpType, const std::string &event, const int32_t &userId,
101         std::vector<std::string> &state);
102 
103     /**
104      * Finishes Receiver.
105      *
106      * @param proxy Indicates the receiver proxy.
107      * @param code Indicates the code of a common event.
108      * @param data Indicates the data of a common event.
109      * @param abortEvent Indicates Whether to cancel the current common event.
110      */
111     void FinishReceiver(
112         const sptr<IRemoteObject> &proxy, const int32_t &code, const std::string &receiverData, const bool &abortEvent);
113 
114     /**
115      * Freezes application.
116      *
117      * @param uid Indicates the uid of application.
118      */
119     void Freeze(const uid_t &uid);
120 
121     /**
122      * Unfreezes application.
123      *
124      * @param uid Indicates the uid of application.
125      */
126     void Unfreeze(const uid_t &uid);
127 
128     /**
129      * Unfreezes all frozen applications.
130      */
131     void UnfreezeAll();
132 
133     /**
134      * dump event for hidumper.
135      *
136      * @param args Indicates the dump options.
137      * @param result the result of dump
138      */
139     void HiDump(const std::vector<std::u16string> &args, std::string &result);
140 
141     /**
142      * Remove sticky common event.
143      *
144      * @param event Name of the common event.
145      * @param callerUid caller uid.
146      * @return Returns ERR_OK if success; otherwise failed.
147      */
148     int32_t RemoveStickyCommonEvent(const std::string &event, uint32_t callerUid);
149 
150     /**
151      * Set Static Subscriber State.
152      *
153      * @param enable static subscriber state.
154      * @return Returns ERR_OK if success; otherwise failed.
155      */
156     int32_t SetStaticSubscriberState(bool enable);
157 
158 private:
159     bool ProcessStickyEvent(const CommonEventRecord &record);
160     bool PublishStickyEvent(const std::shared_ptr<CommonEventSubscribeInfo> &sp,
161         const std::shared_ptr<EventSubscriberRecord> &subscriberRecord);
162     bool CheckUserId(const pid_t &pid, const uid_t &uid, const Security::AccessToken::AccessTokenID &callerToken,
163         EventComeFrom &comeFrom, int32_t &userId);
164     void SendSubscribeHiSysEvent(int32_t userId, const std::string &subscriberName, int32_t pid, int32_t uid,
165         const std::vector<std::string> &events);
166     void SendUnSubscribeHiSysEvent(const sptr<IRemoteObject> &commonEventListener);
167     void SendPublishHiSysEvent(int32_t userId, const std::string &publisherName, int32_t pid, int32_t uid,
168         const std::string &events, bool succeed);
169 
170 private:
171     std::shared_ptr<CommonEventControlManager> controlPtr_;
172     std::shared_ptr<StaticSubscriberManager> staticSubscriberManager_;
173     DISALLOW_COPY_AND_MOVE(InnerCommonEventManager);
174     time_t sysEventTime = 0;
175 };
176 }  // namespace EventFwk
177 }  // namespace OHOS
178 #endif  // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_INNER_COMMON_EVENT_MANAGER_H
179