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 #include "common_event_publish_info.h" 17 #include "event_log_wrapper.h" 18 #include "string_ex.h" 19 20 namespace OHOS { 21 namespace EventFwk { CommonEventPublishInfo()22CommonEventPublishInfo::CommonEventPublishInfo() : sticky_(false), ordered_(false) 23 { 24 } 25 CommonEventPublishInfo(const CommonEventPublishInfo & commonEventPublishInfo)26CommonEventPublishInfo::CommonEventPublishInfo(const CommonEventPublishInfo &commonEventPublishInfo) 27 { 28 sticky_ = commonEventPublishInfo.sticky_; 29 ordered_ = commonEventPublishInfo.ordered_; 30 bundleName_ = commonEventPublishInfo.bundleName_; 31 subscriberPermissions_ = commonEventPublishInfo.subscriberPermissions_; 32 } 33 ~CommonEventPublishInfo()34CommonEventPublishInfo::~CommonEventPublishInfo() 35 { 36 } 37 SetSticky(bool sticky)38void CommonEventPublishInfo::SetSticky(bool sticky) 39 { 40 sticky_ = sticky; 41 } 42 IsSticky() const43bool CommonEventPublishInfo::IsSticky() const 44 { 45 return sticky_; 46 } 47 SetSubscriberPermissions(const std::vector<std::string> & subscriberPermissions)48void CommonEventPublishInfo::SetSubscriberPermissions(const std::vector<std::string> &subscriberPermissions) 49 { 50 subscriberPermissions_ = subscriberPermissions; 51 } 52 GetSubscriberPermissions() const53const std::vector<std::string> &CommonEventPublishInfo::GetSubscriberPermissions() const 54 { 55 return subscriberPermissions_; 56 } 57 SetOrdered(bool ordered)58void CommonEventPublishInfo::SetOrdered(bool ordered) 59 { 60 ordered_ = ordered; 61 } 62 IsOrdered() const63bool CommonEventPublishInfo::IsOrdered() const 64 { 65 return ordered_; 66 } 67 SetBundleName(const std::string & bundleName)68void CommonEventPublishInfo::SetBundleName(const std::string &bundleName) 69 { 70 bundleName_ = bundleName; 71 } 72 GetBundleName() const73std::string CommonEventPublishInfo::GetBundleName() const 74 { 75 return bundleName_; 76 } 77 Marshalling(Parcel & parcel) const78bool CommonEventPublishInfo::Marshalling(Parcel &parcel) const 79 { 80 EVENT_LOGD("enter"); 81 82 // write subscriber permissions 83 std::vector<std::u16string> permissionVec_; 84 for (std::vector<std::string>::size_type i = 0; i < subscriberPermissions_.size(); ++i) { 85 permissionVec_.emplace_back(Str8ToStr16(subscriberPermissions_[i])); 86 } 87 if (!parcel.WriteString16Vector(permissionVec_)) { 88 EVENT_LOGE("common event Publish Info write permission failed"); 89 return false; 90 } 91 92 // write ordered 93 if (!parcel.WriteBool(ordered_)) { 94 EVENT_LOGE("common event Publish Info write ordered failed"); 95 return false; 96 } 97 98 // write sticky 99 if (!parcel.WriteBool(sticky_)) { 100 EVENT_LOGE("common event Publish Info write sticky failed"); 101 return false; 102 } 103 // write bundleName 104 if (!parcel.WriteString16(Str8ToStr16(bundleName_))) { 105 EVENT_LOGE("common event Publish Info write bundleName failed"); 106 return false; 107 } 108 return true; 109 } 110 ReadFromParcel(Parcel & parcel)111bool CommonEventPublishInfo::ReadFromParcel(Parcel &parcel) 112 { 113 EVENT_LOGD("enter"); 114 115 // read subscriber permissions 116 std::vector<std::u16string> permissionVec_; 117 if (!parcel.ReadString16Vector(&permissionVec_)) { 118 EVENT_LOGE("ReadFromParcel read permission error"); 119 return false; 120 } 121 subscriberPermissions_.clear(); 122 for (std::vector<std::u16string>::size_type i = 0; i < permissionVec_.size(); i++) { 123 subscriberPermissions_.emplace_back(Str16ToStr8(permissionVec_[i])); 124 } 125 // read ordered 126 ordered_ = parcel.ReadBool(); 127 // read sticky 128 sticky_ = parcel.ReadBool(); 129 // read bundleName 130 bundleName_ = Str16ToStr8(parcel.ReadString16()); 131 132 return true; 133 } 134 Unmarshalling(Parcel & parcel)135CommonEventPublishInfo *CommonEventPublishInfo::Unmarshalling(Parcel &parcel) 136 { 137 CommonEventPublishInfo *commonEventPublishInfo = new (std::nothrow) CommonEventPublishInfo(); 138 139 if (commonEventPublishInfo == nullptr) { 140 EVENT_LOGE("commonEventPublishInfo == nullptr"); 141 return nullptr; 142 } 143 144 if (!commonEventPublishInfo->ReadFromParcel(parcel)) { 145 EVENT_LOGE("failed to ReadFromParcel"); 146 delete commonEventPublishInfo; 147 commonEventPublishInfo = nullptr; 148 } 149 150 return commonEventPublishInfo; 151 } 152 } // namespace EventFwk 153 } // namespace OHOS