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