1 /*
2  * Copyright (c) 2021-2024 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 "data_ability_result.h"
17 
18 #include <memory>
19 
20 #include "hilog_tag_wrapper.h"
21 #include "parcel_macro_base.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 /**
26  * @brief A constructor used to create a DataAbilityResult instance
27  * with the input parameter count specified.
28  */
DataAbilityResult(int count)29 DataAbilityResult::DataAbilityResult(int count) : uri_("")
30 {
31     count_ = count;
32 }
33 
34 /**
35  * @brief A constructor used to create a DataAbilityResult instance
36  * with a Parcel object specified.
37  */
DataAbilityResult(Parcel & parcel)38 DataAbilityResult::DataAbilityResult(Parcel &parcel) : uri_(""), count_(0)
39 {
40     ReadFromParcel(parcel);
41 }
42 
43 /**
44  * @brief A constructor used to create a DataAbilityResult instance
45  * with the input parameter uri specified
46  */
DataAbilityResult(const Uri & uri)47 DataAbilityResult::DataAbilityResult(const Uri &uri) : uri_(uri.ToString()), count_(0)
48 {}
49 
50 /**
51  * @brief A constructor used to create a DataAbilityResult instance
52  * with input parameters uri, count, and failure specified.
53  */
DataAbilityResult(const Uri & uri,int count)54 DataAbilityResult::DataAbilityResult(const Uri &uri, int count) : uri_(uri.ToString())
55 {
56     count_ = count;
57 }
58 
~DataAbilityResult()59 DataAbilityResult::~DataAbilityResult()
60 {}
61 
62 /**
63  * @brief Obtains the Uri object corresponding to the operation.
64  * @return Obtains the Uri object corresponding to the operation.
65  */
GetUri()66 Uri DataAbilityResult::GetUri()
67 {
68     return uri_;
69 }
70 
71 /**
72  * @brief Obtains the number of rows affected by the operation.
73  * @return Returns the number of rows affected by the operation.
74  */
GetCount()75 int DataAbilityResult::GetCount()
76 {
77     return count_;
78 }
79 
80 /**
81  * @brief Creates a DataAbilityResult instance based on the given Parcel object.
82  * Used to transfer DataAbilityResult object using Parcel.
83  * @param parcel Indicates the Parcel object.
84  * @return Returns the DataAbilityResult object.
85  */
CreateFromParcel(Parcel & parcel)86 DataAbilityResult *DataAbilityResult::CreateFromParcel(Parcel &parcel)
87 {
88     DataAbilityResult *dataAbilityResult = new (std::nothrow) DataAbilityResult(parcel);
89     if (dataAbilityResult == nullptr) {
90         TAG_LOGE(AAFwkTag::DATA_ABILITY, "null dataAbilityResult");
91     }
92     return dataAbilityResult;
93 }
94 
95 /**
96  * @brief Prints out a string containing the class object information.
97  * @return Returns object information.
98  */
ToString()99 std::string DataAbilityResult::ToString()
100 {
101     std::string stringBuilder = "DataAbilityResult(";
102     stringBuilder.append("uri=").append(uri_.ToString()).append(" ");
103     stringBuilder.append("count=").append(std::to_string(count_)).append(" ");
104     stringBuilder.erase(stringBuilder.length() - 1, 1);
105     stringBuilder.append(")");
106     return stringBuilder;
107 }
108 
109 /**
110  * @brief Marshals a DataAbilityResult object into a Parcel.
111  * @param parcel Indicates the Parcel object for marshalling.
112  * @return Returns true if the marshalling is successful; returns false otherwise.
113  */
Marshalling(Parcel & parcel) const114 bool DataAbilityResult::Marshalling(Parcel &parcel) const
115 {
116     // uri_
117     if (uri_.ToString().empty()) {
118         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, VALUE_NULL);
119     } else {
120         if (!parcel.WriteInt32(VALUE_OBJECT)) {
121             return false;
122         }
123         if (!parcel.WriteParcelable(&uri_)) {
124             return false;
125         }
126     }
127 
128     // count_
129     if (!parcel.WriteInt32(count_)) {
130         return false;
131     }
132 
133     return true;
134 }
135 
136 /**
137  * @brief Unmarshals a DataAbilityResult object from a Parcel.
138  * @param parcel Indicates the Parcel object for unmarshalling.
139  * @return Returns true if the unmarshalling is successful; returns false otherwise.
140  */
Unmarshalling(Parcel & parcel)141 DataAbilityResult *DataAbilityResult::Unmarshalling(Parcel &parcel)
142 {
143     DataAbilityResult *dataAbilityResult = new (std::nothrow) DataAbilityResult(0);
144     if (dataAbilityResult != nullptr) {
145         if (!dataAbilityResult->ReadFromParcel(parcel)) {
146             delete dataAbilityResult;
147             dataAbilityResult = nullptr;
148         }
149     }
150 
151     return dataAbilityResult;
152 }
153 
ReadFromParcel(Parcel & parcel)154 bool DataAbilityResult::ReadFromParcel(Parcel &parcel)
155 {
156     // uri_
157     int32_t empty = VALUE_NULL;
158     if (!parcel.ReadInt32(empty)) {
159         return false;
160         TAG_LOGD(AAFwkTag::DATA_ABILITY, "Failed to read parcel");
161     }
162 
163     if (empty == VALUE_OBJECT) {
164         auto uri = parcel.ReadParcelable<Uri>();
165         if (uri != nullptr) {
166             uri_ = *uri;
167             delete uri;
168             uri = nullptr;
169         } else {
170             return false;
171             TAG_LOGD(AAFwkTag::DATA_ABILITY, "Failed to read parcel");
172         }
173     }
174 
175     // count_
176     if (!parcel.ReadInt32(count_)) {
177         return false;
178     }
179 
180     return true;
181 }
182 }  // namespace AppExecFwk
183 }  // namespace OHOS
184