1/*
2 * Copyright (c) 2022-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_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
32namespace OHOS {
33namespace EventFwk {
34class StaticSubscriberManager : public DelayedSingleton<StaticSubscriberManager> {
35public:
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    /**
61     * Set static subscriber state.
62     *
63     * @param events Static subscriber event name.
64     * @param enable Static subscriber state.
65     * @return Returns ERR_OK if success; otherwise failed.
66     */
67    int32_t SetStaticSubscriberState(const std::vector<std::string> &events, bool enable);
68
69private:
70    struct StaticSubscriberInfo {
71        std::string name;
72        std::string bundleName;
73        int32_t userId = -1;
74        std::string permission;
75
76        bool operator==(const StaticSubscriberInfo &that) const
77        {
78            return (name == that.name) && (bundleName == that.bundleName) && (userId == that.userId) &&
79                (permission == that.permission);
80        }
81    };
82
83    struct StaticSubscriber {
84        std::set<std::string> events;
85    };
86
87    bool InitAllowList();
88    bool InitValidSubscribers();
89    void UpdateSubscriber(const CommonEventData &data);
90    void ParseEvents(const std::string &extensionName, const std::string &extensionBundleName,
91        const int32_t &extensionUid, const std::string &profile, bool enable = true);
92    void AddSubscriber(const AppExecFwk::ExtensionAbilityInfo &extension);
93    void AddToValidSubscribers(const std::string &eventName, const StaticSubscriberInfo &extension);
94    void AddSubscriberWithBundleName(const std::string &bundleName, const int32_t &userId);
95    void RemoveSubscriberWithBundleName(const std::string &bundleName, const int32_t &userId);
96    bool VerifySubscriberPermission(const std::string &bundleName, const int32_t &userId,
97        const std::vector<std::string> &permissions);
98    bool VerifyPublisherPermission(const Security::AccessToken::AccessTokenID &callerToken,
99        const std::string &permission);
100    void SendStaticEventProcErrHiSysEvent(int32_t userId, const std::string &publisherName,
101        const std::string &subscriberName, const std::string &eventName);
102    bool IsDisableEvent(const std::string &bundleName, const std::string &event);
103    int32_t UpdateDisableEvents(const std::string &bundleName, const std::vector<std::string> &events, bool enable);
104    void PublishCommonEventConnecAbility(const CommonEventData &data, const sptr<IRemoteObject> &service,
105        const int32_t &userId, const std::string &bundleName, const std::string &abilityName);
106    void PublishCommonEventInner(const CommonEventData &data, const CommonEventPublishInfo &publishInfo,
107        const Security::AccessToken::AccessTokenID &callerToken, const int32_t &userId,
108        const sptr<IRemoteObject> &service, const std::string &bundleName);
109    void SendStaticSubscriberStartHiSysEvent(int32_t userId, const std::string &publisherName,
110        const std::string &subscriberName, const std::string &eventName);
111
112    std::map<std::string, std::vector<StaticSubscriberInfo>> validSubscribers_;
113    std::map<std::string, StaticSubscriber> staticSubscribers_;
114    // key is bundle, value is eventNames
115    std::map<std::string, std::vector<std::string>> disableEvents_;
116    bool hasInitAllowList_ = false;
117    bool hasInitValidSubscribers_ = false;
118    std::mutex subscriberMutex_;
119    std::mutex disableEventsMutex_;
120};
121}  // namespace EventFwk
122}  // namespace OHOS
123
124#endif  // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_STATIC_SUBSCRIBER_MANAGER_H
125