1 /* 2 * Copyright (c) 2022-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_STATIC_SUBSCRIBER_MANAGER_H 17 #define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_STATIC_SUBSCRIBER_MANAGER_H 18 19 #include <map> 20 #include <string> 21 #include <set> 22 #include <vector> 23 24 #include "nlohmann/json.hpp" 25 26 #include "accesstoken_kit.h" 27 #include "bundle_manager_helper.h" 28 #include "common_event_data.h" 29 #include "common_event_publish_info.h" 30 #include "singleton.h" 31 32 namespace OHOS { 33 namespace EventFwk { 34 class StaticSubscriberManager : public DelayedSingleton<StaticSubscriberManager> { 35 public: 36 StaticSubscriberManager(); 37 38 virtual ~StaticSubscriberManager(); 39 40 /** 41 * Publishes common event to the static subscriber. 42 * @param data Indicates the common event data. 43 * @param publishInfo Indicates the publish informattion. 44 * @param callerToken Indicates the caller token. 45 * @param userId Indicates the ID of user. 46 * @param service Indicates the service. 47 */ 48 void PublishCommonEvent(const CommonEventData &data, const CommonEventPublishInfo &publishInfo, 49 const Security::AccessToken::AccessTokenID &callerToken, const int32_t &userId, 50 const sptr<IRemoteObject> &service, const std::string &bundleName); 51 52 /** 53 * Set Static Subscriber State. 54 * 55 * @param enable static subscriber state. 56 * @return Returns ERR_OK if success; otherwise failed. 57 */ 58 int32_t SetStaticSubscriberState(bool enable); 59 60 private: 61 struct StaticSubscriberInfo { 62 std::string name; 63 std::string bundleName; 64 int32_t userId = -1; 65 std::string permission; 66 bool enable = true; 67 68 bool operator==(const StaticSubscriberInfo &that) const 69 { 70 return (name == that.name) && (bundleName == that.bundleName) && (userId == that.userId) && 71 (permission == that.permission) && (enable == that.enable); 72 } 73 }; 74 75 struct StaticSubscriber { 76 std::set<std::string> events; 77 bool enable; 78 }; 79 80 bool InitAllowList(); 81 bool InitValidSubscribers(); 82 void UpdateSubscriber(const CommonEventData &data); 83 void ParseEvents(const std::string &extensionName, const std::string &extensionBundleName, 84 const int32_t &extensionUid, const std::string &profile, bool enable = true); 85 void AddSubscriber(const AppExecFwk::ExtensionAbilityInfo &extension, bool enable = true); 86 void AddToValidSubscribers(const std::string &eventName, const StaticSubscriberInfo &extension); 87 void AddSubscriberWithBundleName(const std::string &bundleName, const int32_t &userId); 88 void RemoveSubscriberWithBundleName(const std::string &bundleName, const int32_t &userId); 89 bool VerifySubscriberPermission(const std::string &bundleName, const int32_t &userId, 90 const std::vector<std::string> &permissions); 91 bool VerifyPublisherPermission(const Security::AccessToken::AccessTokenID &callerToken, 92 const std::string &permission); 93 void SendStaticEventProcErrHiSysEvent(int32_t userId, const std::string &publisherName, 94 const std::string &subscriberName, const std::string &eventName); 95 void InitDisableStaticSubscribersStates( 96 const std::set<std::string> &disableStaticSubscribeAllData, const std::string &bundleName); 97 98 std::map<std::string, std::vector<StaticSubscriberInfo>> validSubscribers_; 99 std::map<std::string, StaticSubscriber> staticSubscribers_; 100 bool hasInitAllowList_ = false; 101 bool hasInitValidSubscribers_ = false; 102 std::mutex subscriberMutex_; 103 }; 104 } // namespace EventFwk 105 } // namespace OHOS 106 107 #endif // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_STATIC_SUBSCRIBER_MANAGER_H 108