• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <vector>
22 
23 #include "nlohmann/json.hpp"
24 
25 #include "accesstoken_kit.h"
26 #include "bundle_manager_helper.h"
27 #include "common_event_data.h"
28 #include "common_event_publish_info.h"
29 #include "singleton.h"
30 
31 namespace OHOS {
32 namespace EventFwk {
33 class StaticSubscriberManager : public DelayedSingleton<StaticSubscriberManager> {
34 public:
35     StaticSubscriberManager();
36 
37     virtual ~StaticSubscriberManager();
38 
39     /**
40      * Publishes common event to the static subscriber.
41      * @param data Indicates the common event data.
42      * @param publishInfo Indicates the publish informattion.
43      * @param callerToken Indicates the caller token.
44      * @param userId Indicates the ID of user.
45      * @param service Indicates the service.
46      */
47     void PublishCommonEvent(const CommonEventData &data, const CommonEventPublishInfo &publishInfo,
48         const Security::AccessToken::AccessTokenID &callerToken, const int32_t &userId,
49         const sptr<IRemoteObject> &service, const std::string &bundleName);
50 
51 private:
52     struct StaticSubscriberInfo {
53         std::string name;
54         std::string bundleName;
55         int32_t userId = -1;
56         std::string permission;
57 
58         bool operator==(const StaticSubscriberInfo &that) const
59         {
60             return (name == that.name) && (bundleName == that.bundleName) && (userId == that.userId) &&
61                 (permission == that.permission);
62         }
63     };
64 
65     bool InitAllowList();
66     bool InitValidSubscribers();
67     void UpdateSubscriber(const CommonEventData &data);
68     void ParseEvents(const std::string &extensionName, const std::string &extensionBundleName,
69         const int32_t &extensionUid, const std::string &profile);
70     void AddSubscriber(const AppExecFwk::ExtensionAbilityInfo &extension);
71     void AddToValidSubscribers(const std::string &eventName, const StaticSubscriberInfo &extension);
72     void AddSubscriberWithBundleName(const std::string &bundleName, const int32_t &userId);
73     void RemoveSubscriberWithBundleName(const std::string &bundleName, const int32_t &userId);
74     bool VerifySubscriberPermission(const std::string &bundleName, const int32_t &userId,
75         const std::vector<std::string> &permissions);
76     bool VerifyPublisherPermission(const Security::AccessToken::AccessTokenID &callerToken,
77         const std::string &permission);
78     void SendStaticEventProcErrHiSysEvent(int32_t userId, const std::string &publisherName,
79         const std::string &subscriberName, const std::string &eventName);
80 
81     std::vector<std::string> subscriberList_;
82     std::map<std::string, std::vector<StaticSubscriberInfo>> validSubscribers_;
83     bool hasInitAllowList_ = false;
84     bool hasInitValidSubscribers_ = false;
85     std::mutex subscriberMutex_;
86 };
87 }  // namespace EventFwk
88 }  // namespace OHOS
89 
90 #endif  // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_STATIC_SUBSCRIBER_MANAGER_H
91