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