• 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 #include "common_event_data.h"
16 
17 #include "event_log_wrapper.h"
18 #include "message_parcel.h"
19 #include "string_ex.h"
20 
21 namespace OHOS {
22 namespace EventFwk {
CommonEventData()23 CommonEventData::CommonEventData() : code_(0)
24 {
25 }
26 
CommonEventData(const Want & want)27 CommonEventData::CommonEventData(const Want &want) : want_(want), code_(0)
28 {
29 }
30 
CommonEventData(const Want & want,const int32_t & code,const std::string & data)31 CommonEventData::CommonEventData(const Want &want, const int32_t &code, const std::string &data)
32     : want_(want), code_(code), data_(data)
33 {
34 }
35 
~CommonEventData()36 CommonEventData::~CommonEventData()
37 {
38 }
39 
SetCode(const int32_t & code)40 void CommonEventData::SetCode(const int32_t &code)
41 {
42     code_ = code;
43 }
44 
GetCode() const45 int32_t CommonEventData::GetCode() const
46 {
47     return code_;
48 }
49 
SetData(const std::string & data)50 void CommonEventData::SetData(const std::string &data)
51 {
52     data_ = data;
53 }
54 
GetData() const55 std::string CommonEventData::GetData() const
56 {
57     return data_;
58 }
59 
SetWant(const Want & want)60 void CommonEventData::SetWant(const Want &want)
61 {
62     want_ = want;
63 }
64 
GetWant() const65 const Want &CommonEventData::GetWant() const
66 {
67     return want_;
68 }
69 
Marshalling(Parcel & parcel) const70 bool CommonEventData::Marshalling(Parcel &parcel) const
71 {
72     // write data
73     if (GetData().empty()) {
74         if (!parcel.WriteInt32(VALUE_NULL)) {
75             EVENT_LOGE("null data");
76             return false;
77         }
78     } else {
79         if (!parcel.WriteInt32(VALUE_OBJECT)) {
80             return false;
81         }
82         if (!parcel.WriteString16(Str8ToStr16(GetData()))) {
83             EVENT_LOGE("Failed to write data");
84             return false;
85         }
86     }
87 
88     if (!parcel.WriteInt32(code_)) {
89         EVENT_LOGE("Failed to write code");
90         return false;
91     }
92 
93     if (!parcel.WriteParcelable(&want_)) {
94         EVENT_LOGE("Failed to write want");
95         return false;
96     }
97 
98     return true;
99 }
100 
ReadFromParcel(Parcel & parcel)101 bool CommonEventData::ReadFromParcel(Parcel &parcel)
102 {
103     // read data
104     int empty = VALUE_NULL;
105     if (!parcel.ReadInt32(empty)) {
106         return false;
107     }
108     if (empty == VALUE_OBJECT) {
109         SetData(Str16ToStr8(parcel.ReadString16()));
110     }
111 
112     // read code
113     code_ = parcel.ReadInt32();
114 
115     // read want
116     std::unique_ptr<Want> want(parcel.ReadParcelable<Want>());
117     if (!want) {
118         EVENT_LOGE("Failed to ReadParcelable<Want>");
119         return false;
120     }
121     want_ = *want;
122 
123     return true;
124 }
125 
Unmarshalling(Parcel & parcel)126 CommonEventData *CommonEventData::Unmarshalling(Parcel &parcel)
127 {
128     CommonEventData *commonEventData = new (std::nothrow) CommonEventData();
129 
130     if (commonEventData == nullptr) {
131         EVENT_LOGE("CommonEventData == nullptr");
132         return nullptr;
133     }
134 
135     if (!commonEventData->ReadFromParcel(parcel)) {
136         EVENT_LOGE("failed to ReadFromParcel");
137         delete commonEventData;
138         commonEventData = nullptr;
139     }
140 
141     return commonEventData;
142 }
143 }  // namespace EventFwk
144 }  // namespace OHOS