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)
27 {
28 }
29
CommonEventSubscribeInfo()30 CommonEventSubscribeInfo::CommonEventSubscribeInfo()
31 : priority_(0), userId_(UNDEFINED_USER), threadMode_(CommonEventSubscribeInfo::ASYNC)
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 }
44
~CommonEventSubscribeInfo()45 CommonEventSubscribeInfo::~CommonEventSubscribeInfo()
46 {
47 EVENT_LOGI("~CommonEventSubscribeInfo");
48 }
49
SetPriority(const int32_t & priority)50 void CommonEventSubscribeInfo::SetPriority(const int32_t &priority)
51 {
52 priority_ = priority;
53 }
54
GetPriority() const55 int32_t CommonEventSubscribeInfo::GetPriority() const
56 {
57 return priority_;
58 }
59
SetUserId(const int32_t & userId)60 void CommonEventSubscribeInfo::SetUserId(const int32_t &userId)
61 {
62 userId_ = userId;
63 }
64
GetUserId() const65 int32_t CommonEventSubscribeInfo::GetUserId() const
66 {
67 return userId_;
68 }
69
SetPermission(const std::string & permission)70 void CommonEventSubscribeInfo::SetPermission(const std::string &permission)
71 {
72 permission_ = permission;
73 }
74
GetPermission() const75 std::string CommonEventSubscribeInfo::GetPermission() const
76 {
77 return permission_;
78 }
79
GetThreadMode() const80 CommonEventSubscribeInfo::ThreadMode CommonEventSubscribeInfo::GetThreadMode() const
81 {
82 return threadMode_;
83 }
84
SetThreadMode(CommonEventSubscribeInfo::ThreadMode threadMode)85 void CommonEventSubscribeInfo::SetThreadMode(CommonEventSubscribeInfo::ThreadMode threadMode)
86 {
87 threadMode_ = threadMode;
88 }
89
SetDeviceId(const std::string & deviceId)90 void CommonEventSubscribeInfo::SetDeviceId(const std::string &deviceId)
91 {
92 deviceId_ = deviceId;
93 }
94
GetDeviceId() const95 std::string CommonEventSubscribeInfo::GetDeviceId() const
96 {
97 return deviceId_;
98 }
99
GetMatchingSkills() const100 const MatchingSkills &CommonEventSubscribeInfo::GetMatchingSkills() const
101 {
102 return matchingSkills_;
103 }
104
Marshalling(Parcel & parcel) const105 bool CommonEventSubscribeInfo::Marshalling(Parcel &parcel) const
106 {
107 // write permission
108 if (!parcel.WriteString16(Str8ToStr16(permission_))) {
109 EVENT_LOGE("Failed to write permission");
110 return false;
111 }
112
113 // write priority
114 if (!parcel.WriteInt32(priority_)) {
115 EVENT_LOGE("Failed to write priority");
116 return false;
117 }
118
119 // write userId
120 if (!parcel.WriteInt32(userId_)) {
121 EVENT_LOGE("Failed to write userId");
122 return false;
123 }
124
125 // write threadMode
126 if (!parcel.WriteUint32(threadMode_)) {
127 EVENT_LOGE("Failed to write threadMode");
128 return false;
129 }
130
131 // write deviceId
132 if (!parcel.WriteString16(Str8ToStr16(deviceId_))) {
133 EVENT_LOGE("Failed to write deviceId");
134 return false;
135 }
136
137 // write matchingSkills
138 if (!parcel.WriteParcelable(&matchingSkills_)) {
139 EVENT_LOGE("Failed to write matchingSkills");
140 return false;
141 }
142
143 return true;
144 }
145
ReadFromParcel(Parcel & parcel)146 bool CommonEventSubscribeInfo::ReadFromParcel(Parcel &parcel)
147 {
148 // read permission
149 permission_ = Str16ToStr8(parcel.ReadString16());
150
151 // read priority
152 priority_ = parcel.ReadInt32();
153
154 // read userId
155 userId_ = parcel.ReadInt32();
156
157 // read threadMode
158 threadMode_ = (CommonEventSubscribeInfo::ThreadMode)parcel.ReadUint32();
159
160 // read deviceId
161 deviceId_ = Str16ToStr8(parcel.ReadString16());
162
163 // read MatchingSkills
164 auto skills = parcel.ReadParcelable<MatchingSkills>();
165 if (skills != nullptr) {
166 matchingSkills_ = *skills;
167 delete skills;
168 } else {
169 EVENT_LOGE("Failed to read matchingSkills");
170 return false;
171 }
172
173 return true;
174 }
175
Unmarshalling(Parcel & parcel)176 CommonEventSubscribeInfo *CommonEventSubscribeInfo::Unmarshalling(Parcel &parcel)
177 {
178 CommonEventSubscribeInfo *commonEventSubscribeInfo = new (std::nothrow) CommonEventSubscribeInfo();
179
180 if (commonEventSubscribeInfo == nullptr) {
181 EVENT_LOGE("commonEventSubscribeInfo == nullptr");
182 return nullptr;
183 }
184 if (commonEventSubscribeInfo && !commonEventSubscribeInfo->ReadFromParcel(parcel)) {
185 EVENT_LOGE("failed to read from parcel");
186 delete commonEventSubscribeInfo;
187 commonEventSubscribeInfo = nullptr;
188 }
189
190 return commonEventSubscribeInfo;
191 }
192 } // namespace EventFwk
193 } // namespace OHOS