1  /*
2   * Copyright (c) 2021-2024 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  #include "nlohmann/json.hpp"
24  
25  namespace OHOS {
26  namespace EventFwk {
27  struct EventComeFrom {
28      bool isSubsystem = false;
29      bool isSystemApp = false;
30      bool isProxy = false;
31      bool isCemShell = false;
32  };
33  
34  class InnerCommonEventManager {
35  public:
36      InnerCommonEventManager();
37  
~InnerCommonEventManager()38      virtual ~InnerCommonEventManager() {};
39  
40      /**
41       * Publishes a common event.
42       *
43       * @param data Indicates the common event data.
44       * @param publishInfo Indicates the publish info.
45       * @param commonEventListener Indicates the common event subscriber.
46       * @param recordTime Indicates the time of record.
47       * @param pid Indicates the pid of application.
48       * @param uid Indicates the uid of application.
49       * @param callerToken Indicates the token of caller.
50       * @param userId Indicates the user ID.
51       * @param bundleName Indicates the name of bundle.
52       * @param service Indicates the common event service.
53       * @return Returns true if successful; false otherwise.
54       */
55      bool PublishCommonEvent(const CommonEventData &data, const CommonEventPublishInfo &publishinfo,
56          const sptr<IRemoteObject> &commonEventListener, const struct tm &recordTime, const pid_t &pid, const uid_t &uid,
57          const Security::AccessToken::AccessTokenID &callerToken, const int32_t &userId, const std::string &bundleName,
58          const sptr<IRemoteObject> &service = nullptr);
59  
60      /**
61       * Subscribes to common events.
62       *
63       * @param subscribeInfo Indicates the subscribe info.
64       * @param commonEventListener Indicates the common event subscriber.
65       * @param recordTime Indicates the time of record.
66       * @param pid Indicates the pid of application.
67       * @param uid Indicates the uid of application.
68       * @param callerToken Indicates the token of caller.
69       * @param bundleName Indicates the name of bundle.
70       * @return Returns true if successful; false otherwise.
71       */
72      bool SubscribeCommonEvent(const CommonEventSubscribeInfo &subscribeInfo,
73          const sptr<IRemoteObject> &commonEventListener, const struct tm &recordTime, const pid_t &pid, const uid_t &uid,
74          const Security::AccessToken::AccessTokenID &callerToken, const std::string &bundleName,
75          const int32_t instanceKey = 0, const int64_t startTime = 0);
76  
77      /**
78       * Unsubscribes from common events.
79       *
80       * @param commonEventListener Indicates the common event subscriber.
81       * @return Returns true if successful; false otherwise.
82       */
83      bool UnsubscribeCommonEvent(const sptr<IRemoteObject> &commonEventListener);
84  
85      /**
86       * Gets the current sticky common event
87       *
88       * @param event Indicates the common event.
89       * @param eventData Indicates the common event data.
90       * @return Returns true if successful; false otherwise.
91       */
92      bool GetStickyCommonEvent(const std::string &event, CommonEventData &eventData);
93  
94      /**
95       * Dumps state of common event service.
96       *
97       * @param dumpType Indicates the dump type.
98       * @param event Specifies the information for the common event. Set null string ("") if you want to dump all.
99       * @param userId Indicates the user ID.
100       * @param state Indicates the state of common event service.
101       */
102      void DumpState(const uint8_t &dumpType, const std::string &event, const int32_t &userId,
103          std::vector<std::string> &state);
104  
105      /**
106       * Finishes Receiver.
107       *
108       * @param proxy Indicates the receiver proxy.
109       * @param code Indicates the code of a common event.
110       * @param data Indicates the data of a common event.
111       * @param abortEvent Indicates Whether to cancel the current common event.
112       */
113      void FinishReceiver(
114          const sptr<IRemoteObject> &proxy, const int32_t &code, const std::string &receiverData, const bool &abortEvent);
115  
116      /**
117       * Freezes application.
118       *
119       * @param uid Indicates the uid of application.
120       */
121      void Freeze(const uid_t &uid);
122  
123      /**
124       * Unfreezes application.
125       *
126       * @param uid Indicates the uid of application.
127       */
128      void Unfreeze(const uid_t &uid);
129  
130      /**
131       * Unfreezes all frozen applications.
132       */
133      void UnfreezeAll();
134  
135      /**
136       * dump event for hidumper.
137       *
138       * @param args Indicates the dump options.
139       * @param result the result of dump
140       */
141      void HiDump(const std::vector<std::u16string> &args, std::string &result);
142  
143      /**
144       * Remove sticky common event.
145       *
146       * @param event Name of the common event.
147       * @param callerUid caller uid.
148       * @return Returns ERR_OK if success; otherwise failed.
149       */
150      int32_t RemoveStickyCommonEvent(const std::string &event, uint32_t callerUid);
151  
152      /**
153       * Set Static Subscriber State.
154       *
155       * @param enable static subscriber state.
156       * @return Returns ERR_OK if success; otherwise failed.
157       */
158      int32_t SetStaticSubscriberState(bool enable);
159  
160      /**
161       * Set static subscriber state.
162       *
163       * @param events Static subscriber event name.
164       * @param enable Static subscriber state.
165       * @return Returns ERR_OK if success; otherwise failed.
166       */
167      int32_t SetStaticSubscriberState(const std::vector<std::string> &events, bool enable);
168  
169      /**
170      * Set freeze status of process.
171      *
172      * @param pidList Indicates the list of process id.
173      * @param isFreeze Indicates wheather the process is freezed.
174      * @return Returns true if successful; false otherwise.
175      */
176      bool SetFreezeStatus(std::set<int> pidList, bool isFreeze);
177  
178  private:
179      bool ProcessStickyEvent(const CommonEventRecord &record);
180      bool PublishStickyEvent(const std::shared_ptr<CommonEventSubscribeInfo> &sp,
181          const std::shared_ptr<EventSubscriberRecord> &subscriberRecord);
182      bool CheckUserId(const pid_t &pid, const uid_t &uid, const Security::AccessToken::AccessTokenID &callerToken,
183          EventComeFrom &comeFrom, int32_t &userId);
184      void SendSubscribeHiSysEvent(int32_t userId, const std::string &subscriberName, int32_t pid, int32_t uid,
185          const std::vector<std::string> &events);
186      void SendUnSubscribeHiSysEvent(const sptr<IRemoteObject> &commonEventListener);
187      void SendPublishHiSysEvent(int32_t userId, const std::string &publisherName, int32_t pid, int32_t uid,
188          const std::string &events, bool succeed);
189      void SetSystemUserId(const uid_t &uid, EventComeFrom &comeFrom, int32_t &userId);
190      bool GetJsonFromFile(const char *path, nlohmann::json &root);
191      bool GetJsonByFilePath(const char *filePath, std::vector<nlohmann::json> &roots);
192      bool GetConfigJson(const std::string &keyCheck, nlohmann::json &configJson) const;
193      void getCcmPublishControl();
194      bool IsPublishAllowed(const std::string &event, int32_t uid);
195  
196  private:
197      std::shared_ptr<CommonEventControlManager> controlPtr_;
198      std::shared_ptr<StaticSubscriberManager> staticSubscriberManager_;
199      DISALLOW_COPY_AND_MOVE(InnerCommonEventManager);
200      time_t sysEventTime = 0;
201      std::string supportCheckSaPermission_ = "false";
202      std::atomic<int> subCount = 0;
203      std::unordered_map<std::string, std::vector<int32_t>> publishControlMap_;
204      std::vector<nlohmann::json> eventConfigJson_;
205  };
206  }  // namespace EventFwk
207  }  // namespace OHOS
208  #endif  // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_INNER_COMMON_EVENT_MANAGER_H
209