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