1 /* 2 * Copyright (c) 2021 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_KITS_NATIVE_INCLUDE_PUBLISH_INFO_H 17 #define FOUNDATION_EVENT_CESFWK_KITS_NATIVE_INCLUDE_PUBLISH_INFO_H 18 19 #include "parcel.h" 20 21 namespace OHOS { 22 namespace EventFwk { 23 24 enum SubscriberType { 25 ALL_SUBSCRIBER_TYPE, 26 SYSTEM_SUBSCRIBER_TYPE 27 }; 28 29 enum class ValidationRule { 30 AND = 1, 31 OR = 2, 32 }; 33 34 static constexpr uint16_t SUBSCRIBER_FILTER_BUNDLE_INDEX = 1 << 0; 35 static constexpr uint16_t SUBSCRIBER_FILTER_PERMISSION_INDEX = 1 << 1; 36 static constexpr uint16_t SUBSCRIBER_FILTER_SUBSCRIBER_TYPE_INDEX = 1 << 2; 37 static constexpr uint16_t SUBSCRIBER_FILTER_SUBSCRIBER_UID_INDEX = 1 << 3; 38 static constexpr int32_t UNINITIALIZATED_SUBSCRIBER_TYPE = -1; 39 40 class CommonEventPublishInfo : public Parcelable { 41 public: 42 CommonEventPublishInfo(); 43 44 /** 45 * A constructor used to create a CommonEventPublishInfo instance by copying 46 * parameters from an existing one. 47 * 48 * @param commonEventPublishInfo Indicates the publish info. 49 */ 50 explicit CommonEventPublishInfo(const CommonEventPublishInfo &commonEventPublishInfo); 51 52 ~CommonEventPublishInfo(); 53 54 /** 55 * Sets whether the type of a common event is sticky or not. 56 * 57 * @param sticky Indicates the type of a common event is sticky or not 58 */ 59 void SetSticky(bool sticky); 60 61 /** 62 * Obtains whether it is a sticky common event, which can be set 63 * by SetSticky(bool). 64 * 65 * @return Returns the common event is sticky or not. 66 */ 67 bool IsSticky() const; 68 69 /** 70 * Sets permissions for subscribers. 71 * 72 * @param subscriberPermissions Indicates the permissions for subscribers. 73 */ 74 void SetSubscriberPermissions(const std::vector<std::string> &subscriberPermissions); 75 76 /** 77 * Obtains subscriber permissions to a common event, which can be set by 78 * setSubscriberPermissions(const std::vector<std::string>&). 79 * 80 * @return Returns the permissions for subscribers. 81 */ 82 const std::vector<std::string> &GetSubscriberPermissions() const; 83 84 /** 85 * Sets whether the type of a common event is ordered or not. 86 * 87 * @param ordered Indicates the type of a common event is ordered or not. 88 */ 89 void SetOrdered(bool ordered); 90 91 /** 92 * 93 * Obtains whether it is an ordered common event, which can be set by setOrdered(boolean). 94 * set by SetOrdered(bool). 95 * 96 * @return Returns the type of a common event is ordered or not. 97 */ 98 bool IsOrdered() const; 99 100 /** 101 * Sets the bundleName. 102 * 103 * @param bundleName Indicates the bundleName of a common event. 104 */ 105 void SetBundleName(const std::string &bundleName); 106 107 /** 108 * Sets uid of subscriber. A maximum of three receiver UIDs are supported. 109 * If there are more subscribers, the publisher needs to control the permission. 110 * 111 * @param subscriberUids Indicates the uids of subscribers. 112 */ 113 void SetSubscriberUid(const std::vector<int32_t> &subscriberUids); 114 115 /** 116 * Obtains the uids of subscribers of this CommonEventPublishInfo object. 117 * 118 * @return Returns the uids of subscribers. 119 */ 120 std::vector<int32_t> GetSubscriberUid() const; 121 122 /** 123 * Sets type of subscriber. 124 * 125 * @param subscriberType Indicates the type of subscriber, which can be ALL_SUBSCRIBER_TYPE = 0 126 * or SYSTEM_SUBSCRIBER_TYPE = 1. default is ALL_SUBSCRIBER_TYPE when param is out of range. 127 */ 128 void SetSubscriberType(const int32_t &subscriberType); 129 130 /** 131 * Obtains the type of subscriber of this CommonEventPublishInfo object. 132 * 133 * @return Returns the type of subscriber. 134 */ 135 int32_t GetSubscriberType() const; 136 137 /** 138 * Obtains the bundleName of a common event 139 * 140 * @return Returns the bundleName of a common event. 141 */ 142 std::string GetBundleName() const; 143 144 /** 145 * Sets validationRule of subscriber. 146 * 147 * @param rule Indicates the validation rule of subscriber, which can be AND or OR. default is AND. 148 */ 149 void SetValidationRule(const ValidationRule &rule); 150 151 /** 152 * Obtains the validationRule of a common event 153 * 154 * @return Returns the validationRule of a common event. 155 */ 156 ValidationRule GetValidationRule() const; 157 158 /** 159 * Obtains the settings of filter parameters 160 * SUBSCRIBER_FILTER_BUNDLE_INDEX = 1 << 0; 161 * SUBSCRIBER_FILTER_PERMISSION_INDEX = 1 << 1; 162 * SUBSCRIBER_FILTER_SUBSCRIBER_TYPE_INDEX = 1 << 2; 163 * SUBSCRIBER_FILTER_SUBSCRIBER_UID_INDEX = 1 << 3; 164 * 165 * @return Returns the bits of filter settings. 166 */ 167 uint16_t GetFilterSettings() const; 168 169 /** 170 * Marshals a CommonEventData object into a Parcel. 171 * 172 * @param parcel Indicates specified Parcel object. 173 * @return Returns true if success; false otherwise. 174 */ 175 virtual bool Marshalling(Parcel &parcel) const override; 176 177 /** 178 * UnMarshals a Parcel object into a CommonEventData. 179 * 180 * @return Returns the common event data. 181 */ 182 static CommonEventPublishInfo *Unmarshalling(Parcel &parcel); 183 184 private: 185 /** 186 * Reads a CommonEventData object from a Parcel. 187 * 188 * @param parcel Indicates specified Parcel object. 189 */ 190 bool ReadFromParcel(Parcel &parcel); 191 192 bool isSubscriberType(int32_t subsciberType); 193 194 private: 195 bool sticky_; 196 bool ordered_; 197 std::string bundleName_; 198 std::vector<std::string> subscriberPermissions_; 199 std::vector<int32_t> subscriberUids_; 200 int32_t subscriberType_; 201 ValidationRule rule_; 202 }; 203 } // namespace EventFwk 204 } // namespace OHOS 205 206 #endif // FOUNDATION_EVENT_CESFWK_KITS_NATIVE_INCLUDE_PUBLISH_INFO_H