• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include "common_event_subscribe_info.h"
17 
18 #include "common_event_constant.h"
19 #include "event_log_wrapper.h"
20 #include "string_ex.h"
21 
22 namespace OHOS {
23 namespace EventFwk {
CommonEventSubscribeInfo(const MatchingSkills & matchingSkills)24 CommonEventSubscribeInfo::CommonEventSubscribeInfo(const MatchingSkills &matchingSkills)
25     : matchingSkills_(matchingSkills), priority_(0), userId_(UNDEFINED_USER),
26     threadMode_(CommonEventSubscribeInfo::ASYNC), publisherUid_(0)
27 {
28 }
29 
CommonEventSubscribeInfo()30 CommonEventSubscribeInfo::CommonEventSubscribeInfo()
31     : priority_(0), userId_(UNDEFINED_USER), threadMode_(CommonEventSubscribeInfo::ASYNC), publisherUid_(0)
32 {
33 }
34 
CommonEventSubscribeInfo(const CommonEventSubscribeInfo & commonEventSubscribeInfo)35 CommonEventSubscribeInfo::CommonEventSubscribeInfo(const CommonEventSubscribeInfo &commonEventSubscribeInfo)
36 {
37     matchingSkills_ = commonEventSubscribeInfo.matchingSkills_;
38     priority_ = commonEventSubscribeInfo.priority_;
39     userId_ = commonEventSubscribeInfo.userId_;
40     permission_ = commonEventSubscribeInfo.permission_;
41     deviceId_ = commonEventSubscribeInfo.deviceId_;
42     threadMode_ = commonEventSubscribeInfo.threadMode_;
43     publisherBundleName_ = commonEventSubscribeInfo.publisherBundleName_;
44     publisherUid_ = commonEventSubscribeInfo.publisherUid_;
45 }
46 
~CommonEventSubscribeInfo()47 CommonEventSubscribeInfo::~CommonEventSubscribeInfo()
48 {
49 }
50 
SetPriority(const int32_t & priority)51 void CommonEventSubscribeInfo::SetPriority(const int32_t &priority)
52 {
53     priority_ = priority;
54 }
55 
GetPriority() const56 int32_t CommonEventSubscribeInfo::GetPriority() const
57 {
58     return priority_;
59 }
60 
SetUserId(const int32_t & userId)61 void CommonEventSubscribeInfo::SetUserId(const int32_t &userId)
62 {
63     userId_ = userId;
64 }
65 
GetUserId() const66 int32_t CommonEventSubscribeInfo::GetUserId() const
67 {
68     return userId_;
69 }
70 
SetPermission(const std::string & permission)71 void CommonEventSubscribeInfo::SetPermission(const std::string &permission)
72 {
73     permission_ = permission;
74 }
75 
GetPermission() const76 std::string CommonEventSubscribeInfo::GetPermission() const
77 {
78     return permission_;
79 }
80 
GetThreadMode() const81 CommonEventSubscribeInfo::ThreadMode CommonEventSubscribeInfo::GetThreadMode() const
82 {
83     return threadMode_;
84 }
85 
SetThreadMode(CommonEventSubscribeInfo::ThreadMode threadMode)86 void CommonEventSubscribeInfo::SetThreadMode(CommonEventSubscribeInfo::ThreadMode threadMode)
87 {
88     threadMode_ = threadMode;
89 }
90 
SetDeviceId(const std::string & deviceId)91 void CommonEventSubscribeInfo::SetDeviceId(const std::string &deviceId)
92 {
93     deviceId_ = deviceId;
94 }
95 
GetDeviceId() const96 std::string CommonEventSubscribeInfo::GetDeviceId() const
97 {
98     return deviceId_;
99 }
100 
GetMatchingSkills() const101 const MatchingSkills &CommonEventSubscribeInfo::GetMatchingSkills() const
102 {
103     return matchingSkills_;
104 }
105 
SetPublisherBundleName(const std::string & publisherBundleName)106 void CommonEventSubscribeInfo::SetPublisherBundleName(const std::string &publisherBundleName)
107 {
108     publisherBundleName_ = publisherBundleName;
109 }
110 
GetPublisherBundleName() const111 std::string CommonEventSubscribeInfo::GetPublisherBundleName() const
112 {
113     return publisherBundleName_;
114 }
115 
SetPublisherUid(int32_t publisherUid)116 void CommonEventSubscribeInfo::SetPublisherUid(int32_t publisherUid)
117 {
118     publisherUid_ = publisherUid;
119 }
120 
GetPublisherUid() const121 int32_t CommonEventSubscribeInfo::GetPublisherUid() const
122 {
123     return publisherUid_;
124 }
125 
Marshalling(Parcel & parcel) const126 bool CommonEventSubscribeInfo::Marshalling(Parcel &parcel) const
127 {
128     // write permission
129     if (!parcel.WriteString16(Str8ToStr16(permission_))) {
130         EVENT_LOGE("Failed to write permission");
131         return false;
132     }
133 
134     // write priority
135     if (!parcel.WriteInt32(priority_)) {
136         EVENT_LOGE("Failed to write priority");
137         return false;
138     }
139 
140     // write userId
141     if (!parcel.WriteInt32(userId_)) {
142         EVENT_LOGE("Failed to write userId");
143         return false;
144     }
145 
146     // write threadMode
147     if (!parcel.WriteUint32(threadMode_)) {
148         EVENT_LOGE("Failed to write threadMode");
149         return false;
150     }
151 
152     // write deviceId
153     if (!parcel.WriteString16(Str8ToStr16(deviceId_))) {
154         EVENT_LOGE("Failed to write deviceId");
155         return false;
156     }
157 
158     // write matchingSkills
159     if (!parcel.WriteParcelable(&matchingSkills_)) {
160         EVENT_LOGE("Failed to write matchingSkills");
161         return false;
162     }
163 
164     // write publisherBundleName
165     if (!parcel.WriteString(publisherBundleName_)) {
166         EVENT_LOGE("Failed to write publisherBundleName");
167         return false;
168     }
169 
170     // write publisherUid
171     if (!parcel.WriteInt32(publisherUid_)) {
172         EVENT_LOGE("Failed to write publisherUid");
173         return false;
174     }
175 
176     return true;
177 }
178 
ReadFromParcel(Parcel & parcel)179 bool CommonEventSubscribeInfo::ReadFromParcel(Parcel &parcel)
180 {
181     // read permission
182     permission_ = Str16ToStr8(parcel.ReadString16());
183 
184     // read priority
185     priority_ = parcel.ReadInt32();
186 
187     // read userId
188     userId_ = parcel.ReadInt32();
189 
190     // read threadMode
191     threadMode_ = (CommonEventSubscribeInfo::ThreadMode)parcel.ReadUint32();
192 
193     // read deviceId
194     deviceId_ = Str16ToStr8(parcel.ReadString16());
195 
196     // read MatchingSkills
197     auto skills = parcel.ReadParcelable<MatchingSkills>();
198     if (skills != nullptr) {
199         matchingSkills_ = *skills;
200         delete skills;
201     } else {
202         EVENT_LOGE("Failed to read matchingSkills");
203         return false;
204     }
205 
206     // read publisherBundleName
207     publisherBundleName_ = parcel.ReadString();
208 
209     // read publisherUid
210     publisherUid_ = parcel.ReadInt32();
211 
212     return true;
213 }
214 
Unmarshalling(Parcel & parcel)215 CommonEventSubscribeInfo *CommonEventSubscribeInfo::Unmarshalling(Parcel &parcel)
216 {
217     CommonEventSubscribeInfo *commonEventSubscribeInfo = new (std::nothrow) CommonEventSubscribeInfo();
218 
219     if (commonEventSubscribeInfo == nullptr) {
220         EVENT_LOGE("commonEventSubscribeInfo == nullptr");
221         return nullptr;
222     }
223     if (commonEventSubscribeInfo && !commonEventSubscribeInfo->ReadFromParcel(parcel)) {
224         EVENT_LOGE("failed to read from parcel");
225         delete commonEventSubscribeInfo;
226         commonEventSubscribeInfo = nullptr;
227     }
228 
229     return commonEventSubscribeInfo;
230 }
231 }  // namespace EventFwk
232 }  // namespace OHOS