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_COMMON_EVENT_MANAGER_SERVICE_H 17 #define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_MANAGER_SERVICE_H 18 19 #include "common_event_stub.h" 20 #include "event_handler.h" 21 #include "ffrt.h" 22 #include "inner_common_event_manager.h" 23 #include "nocopyable.h" 24 #include "refbase.h" 25 #include <mutex> 26 27 namespace OHOS { 28 namespace EventFwk { 29 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 30 31 class CommonEventManagerService : public CommonEventStub { 32 public: 33 static sptr<CommonEventManagerService> GetInstance(); 34 CommonEventManagerService(); 35 virtual ~CommonEventManagerService(); 36 /** 37 * Publishes a common event. 38 * 39 * @param event Indicates the common event data. 40 * @param publishInfo Indicates the publish info. 41 * @param commonEventListener Indicates the common event subscriber object. 42 * @param userId Indicates the user ID. 43 * @return Returns true if successful; false otherwise. 44 */ 45 int32_t PublishCommonEvent(const CommonEventData &event, const CommonEventPublishInfo &publishinfo, 46 const sptr<IRemoteObject> &commonEventListener, const int32_t &userId) override; 47 48 /** 49 * Publishes a common event. 50 * 51 * @param event Indicates the common event data. 52 * @param publishInfo Indicates the publish info. 53 * @param commonEventListener Indicates the common event subscriber. 54 * @param uid Indicates the uid of application. 55 * @param callerToken Indicates the caller token 56 * @param userId Indicates the user ID. 57 * @return Returns true if successful; false otherwise. 58 */ 59 bool PublishCommonEvent(const CommonEventData &event, const CommonEventPublishInfo &publishinfo, 60 const sptr<IRemoteObject> &commonEventListener, const uid_t &uid, const int32_t &callerToken, 61 const int32_t &userId) override; 62 63 /** 64 * Subscribes to common events. 65 * 66 * @param subscribeInfo Indicates the subscribe info. 67 * @param commonEventListener Indicates the common event subscriber. 68 * @param instanceKey Indicates the instance key 69 * @return Returns true if successful; false otherwise. 70 */ 71 int32_t SubscribeCommonEvent(const CommonEventSubscribeInfo &subscribeInfo, 72 const sptr<IRemoteObject> &commonEventListener, const int32_t instanceKey = 0) override; 73 74 /** 75 * Unsubscribes from common events. 76 * 77 * @param commonEventListener Indicates the common event subscriber. 78 * @return Returns true if successful; false otherwise. 79 */ 80 int32_t UnsubscribeCommonEvent(const sptr<IRemoteObject> &commonEventListener) override; 81 82 /** 83 * Synchronized, unsubscribes from common events. 84 * 85 * @param commonEventListener Indicates the common event subscriber. 86 * @return Returns true if successful; false otherwise. 87 */ 88 int32_t UnsubscribeCommonEventSync(const sptr<IRemoteObject> &commonEventListener) override; 89 90 /** 91 * Gets the current sticky common event 92 * 93 * @param event Indicates the common event. 94 * @param eventData Indicates the common event data. 95 * @return Returns true if successful; false otherwise. 96 */ 97 bool GetStickyCommonEvent(const std::string &event, CommonEventData &eventData) override; 98 99 /** 100 * Dumps state of common event service. 101 * 102 * @param dumpType Indicates the dump type. 103 * @param event Specifies the information for the common event. Set null string ("") if you want to dump all. 104 * @param userId Indicates the user ID. 105 * @param state Indicates the state of common event service. 106 * @return Returns true if successful; false otherwise. 107 */ 108 bool DumpState(const uint8_t &dumpType, const std::string &event, const int32_t &userId, 109 std::vector<std::string> &state) override; 110 111 /** 112 * Finishes Receiver. 113 * 114 * @param proxy Indicates the receiver proxy. 115 * @param code Indicates the code of a common event. 116 * @param data Indicates the data of a common event. 117 * @param abortEvent Indicates Whether to cancel the current common event. 118 * @return Returns true if successful; false otherwise. 119 */ 120 bool FinishReceiver(const sptr<IRemoteObject> &proxy, const int32_t &code, const std::string &receiverData, 121 const bool &abortEvent) override; 122 123 /** 124 * Freezes application. 125 * 126 * @param uid Indicates the uid of application. 127 * @return Returns true if successful; false otherwise. 128 */ 129 bool Freeze(const uid_t &uid) override; 130 131 /** 132 * Unfreezes application. 133 * 134 * @param uid Indicates the Uid of application. 135 * @return Returns true if successful; false otherwise. 136 */ 137 bool Unfreeze(const uid_t &uid) override; 138 139 /** 140 * Unfreezes all frozen applications. 141 * 142 * @return Returns true if successful; false otherwise. 143 */ 144 bool UnfreezeAll() override; 145 146 /** 147 * Remove sticky common event. 148 * 149 * @param event Name of the common event. 150 * @return Returns ERR_OK if success; otherwise failed. 151 */ 152 int32_t RemoveStickyCommonEvent(const std::string &event) override; 153 154 /** 155 * Set Static Subscriber State. 156 * 157 * @param enable static subscriber state. 158 * @return Returns ERR_OK if success; otherwise failed. 159 */ 160 int32_t SetStaticSubscriberState(bool enable) override; 161 162 /** 163 * Set static subscriber state. 164 * 165 * @param events Static subscriber event name. 166 * @param enable Static subscriber state. 167 * @return Returns ERR_OK if success; otherwise failed. 168 */ 169 int32_t SetStaticSubscriberState(const std::vector<std::string> &events, bool enable) override; 170 171 /** 172 * Set freeze status of process. 173 * 174 * @param pidList Indicates the list of process id. 175 * @param isFreeze Indicates wheather the process is freezed. 176 * @return Returns true if successful; false otherwise. 177 */ 178 bool SetFreezeStatus(std::set<int> pidList, bool isFreeze) override; 179 180 int Dump(int fd, const std::vector<std::u16string> &args) override; 181 182 ErrCode Init(); 183 184 private: 185 bool IsReady() const; 186 187 int32_t PublishCommonEventDetailed(const CommonEventData &event, const CommonEventPublishInfo &publishinfo, 188 const sptr<IRemoteObject> &commonEventListener, const pid_t &pid, const uid_t &uid, 189 const int32_t &clientToken, const int32_t &userId); 190 191 void GetHidumpInfo(const std::vector<std::u16string> &args, std::string &result); 192 int32_t CheckUserIdParams(const int32_t &userId); 193 private: 194 static sptr<CommonEventManagerService> instance_; 195 static std::mutex instanceMutex_; 196 197 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager_; 198 ServiceRunningState serviceRunningState_ = ServiceRunningState::STATE_NOT_START; 199 std::shared_ptr<EventRunner> runner_; 200 std::shared_ptr<EventHandler> handler_; 201 std::shared_ptr<ffrt::queue> commonEventSrvQueue_ = nullptr; 202 std::string supportCheckSaPermission_ = "false"; 203 204 DISALLOW_COPY_AND_MOVE(CommonEventManagerService); 205 }; 206 } // namespace EventFwk 207 } // namespace OHOS 208 209 #endif // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_MANAGER_SERVICE_H