• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 #include "ffrt.h"
32 
33 namespace OHOS {
34 namespace EventFwk {
35 class StaticSubscriberManager : public DelayedSingleton<StaticSubscriberManager> {
36 public:
37     StaticSubscriberManager();
38 
39     virtual ~StaticSubscriberManager();
40 
41     /**
42      * Publishes common event to the static subscriber.
43      * @param data Indicates the common event data.
44      * @param publishInfo Indicates the publish informattion.
45      * @param callerToken Indicates the caller token.
46      * @param userId Indicates the ID of user.
47      * @param service Indicates the service.
48      */
49     void PublishCommonEvent(const CommonEventData &data, const CommonEventPublishInfo &publishInfo,
50         const Security::AccessToken::AccessTokenID &callerToken, const int32_t &userId,
51         const sptr<IRemoteObject> &service, const std::string &bundleName);
52 
53     /**
54      * Set Static Subscriber State.
55      *
56      * @param enable static subscriber state.
57      * @return Returns ERR_OK if success; otherwise failed.
58      */
59     int32_t SetStaticSubscriberState(bool enable);
60 
61     /**
62      * Set static subscriber state.
63      *
64      * @param events Static subscriber event name.
65      * @param enable Static subscriber state.
66      * @return Returns ERR_OK if success; otherwise failed.
67      */
68     int32_t SetStaticSubscriberState(const std::vector<std::string> &events, bool enable);
69 
70     using ParameterType = std::variant<bool, int32_t, double, std::string>;
71 
72 private:
73     struct StaticSubscriberInfo {
74         std::string name;
75         std::string bundleName;
76         int32_t userId = -1;
77         std::string permission;
78         std::optional<int32_t> filterCode;
79         std::optional<std::string> filterData;
80         std::map<std::string, ParameterType> filterParameters;
81 
82         bool operator==(const StaticSubscriberInfo &that) const
83         {
84             return (name == that.name) && (bundleName == that.bundleName) && (userId == that.userId) &&
85                 (permission == that.permission);
86         }
87     };
88 
89     struct StaticSubscriber {
90         std::set<std::string> events;
91     };
92 
93     bool InitAllowList();
94     bool InitValidSubscribers();
95     void UpdateSubscriber(const CommonEventData &data);
96     void ParseEvents(const std::string &extensionName, const std::string &extensionBundleName,
97         const int32_t &extensionUid, const std::string &profile, bool enable = true);
98     void AddSubscriber(const AppExecFwk::ExtensionAbilityInfo &extension);
99     void AddToValidSubscribers(const std::string &eventName, const StaticSubscriberInfo &extension);
100     void AddSubscriberWithBundleName(const std::string &bundleName, const int32_t &userId);
101     void RemoveSubscriberWithBundleName(const std::string &bundleName, const int32_t &userId);
102     bool VerifySubscriberPermission(const std::string &bundleName, const int32_t &userId,
103         const std::vector<std::string> &permissions);
104     bool VerifyPublisherPermission(const Security::AccessToken::AccessTokenID &callerToken,
105         const std::string &permission);
106     void SendStaticEventProcErrHiSysEvent(int32_t userId, const std::string &publisherName,
107         const std::string &subscriberName, const std::string &eventName);
108     bool IsDisableEvent(const std::string &bundleName, const std::string &event);
109     int32_t UpdateDisableEvents(const std::string &bundleName, const std::vector<std::string> &events, bool enable);
110     void PublishCommonEventConnecAbility(const CommonEventData &data, const sptr<IRemoteObject> &service,
111         const int32_t &userId, const std::string &bundleName, const std::string &abilityName);
112     void PublishCommonEventInner(const CommonEventData &data, const CommonEventPublishInfo &publishInfo,
113         const Security::AccessToken::AccessTokenID &callerToken, const int32_t &userId,
114         const sptr<IRemoteObject> &service, const std::string &bundleName);
115     void SendStaticSubscriberStartHiSysEvent(int32_t userId, const std::string &publisherName,
116         const std::string &subscriberName, const std::string &eventName);
117     void ParseFilterObject(
118         const nlohmann::json &filterObj, const std::string &eventName, StaticSubscriberInfo &subscriber);
119     void ParseConditions(
120         const nlohmann::json &conditions, const std::string &eventName, StaticSubscriberInfo &subscriber);
121     void AddFilterParameter(const std::string &paramName, const nlohmann::json &paramValue,
122         std::map<std::string, ParameterType> &filterParameters);
123     bool IsFilterParameters(const StaticSubscriberInfo &staticSubscriberInfo, const CommonEventData &data) const;
124     bool CheckFilterCodeAndData(const StaticSubscriberInfo &staticSubscriberInfo, const CommonEventData &data) const;
125     bool CheckFilterParameters(const std::map<std::string, ParameterType> &filterParameters, const Want &want) const;
126     template<typename T, typename QueryFunc, typename UnboxFunc>
127     bool CheckSpecificParam(const std::string &paramName, const T &paramValue, const Want &want, QueryFunc queryFunc,
128         UnboxFunc unboxFunc) const;
129     bool CheckSubscriberWhetherMatched(const StaticSubscriberInfo &subscriber,
130         const CommonEventPublishInfo &publishInfo);
131     bool CheckSubscriberBySpecifiedUids(const int32_t &subscriberUid,
132         const std::vector<int32_t> &specifiedSubscriberUids);
133     std::map<std::string, std::vector<StaticSubscriberInfo>> validSubscribers_;
134     std::map<std::string, StaticSubscriber> staticSubscribers_;
135     // key is bundle, value is eventNames
136     std::map<std::string, std::vector<std::string>> disableEvents_;
137     bool hasInitAllowList_ = false;
138     bool hasInitValidSubscribers_ = false;
139     ffrt::mutex subscriberMutex_;
140     ffrt::mutex disableEventsMutex_;
141     std::shared_ptr<ffrt::queue> ffrt_ = nullptr;
142 };
143 }  // namespace EventFwk
144 }  // namespace OHOS
145 
146 #endif  // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_STATIC_SUBSCRIBER_MANAGER_H
147